Skip to content

Commit

Permalink
feat: add removeCompleted to config
Browse files Browse the repository at this point in the history
closes #106
  • Loading branch information
shadowgate15 committed Feb 21, 2022
1 parent 247795b commit 139a949
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ Here is the full list of options and their defaults. See [index.js](index.js) f
| `root` | String | `path.resolve('jobs')` | Resolves a jobs folder relative to where the project is ran (the directory you call `node` in). Set this value to `false` to prevent requiring a root directory of jobs (e.g. if your jobs are not all in one directory). Set this to `path.join(__dirname, 'jobs')` to keep your jobs directory relative to the file where Bree is set up. |
| `silenceRootCheckError` | Boolean | `false` | Silences errors from requiring the root folder. Set this to `false` if you do not want to see errors from this operation |
| `doRootCheck` | Boolean | `true` | Attempts to `require` the root directory, when `jobs` is empty or `null`. Set this to `false` to prevent requiring the root directory |
| `removeCompleted` | Boolean | `false` | Removes job upon completion. Set this to `true` in order to remove jobs from the array upon completion. |
| `timeout` | Number | `0` | Default timeout for jobs (e.g. a value of `0` means that jobs will start on boot by default unless a job has a property of `timeout` or `interval` defined. Set this to `false` if you do not wish for a default value to be set for jobs. **This value does not apply to jobs with a property of `date`.** |
| `interval` | Number | `0` | Default interval for jobs (e.g. a value of `0` means that there is no interval, and a value greater than zero indicates a default interval will be set with this value). **This value does not apply to jobs with a property of `cron`**. |
| `jobs` | Array | `[]` | Defaults to an empty Array, but if the `root` directory has a `index.js` file, then it will be used. This allows you to keep your jobs and job definition index in the same place. See [Job Options](#job-options) below, and [Usage and Examples](#usage-and-examples) above for more insight. |
Expand Down
24 changes: 20 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Bree extends EventEmitter {
silenceRootCheckError: false,
// Set this to `false` to prevent requiring a root directory of jobs
doRootCheck: true,
// Remove jobs upon completion
// (set this to `true` if you want jobs to removed from array upon completion)
// this will not remove jobs when `stop` is called
removeCompleted: false,
// Default timeout for jobs
// (set this to `false` if you do not wish for a default timeout to be set)
timeout: 0,
Expand Down Expand Up @@ -156,6 +160,7 @@ class Bree extends EventEmitter {
this.add = this.add.bind(this);
this.remove = this.remove.bind(this);
this.removeSafeTimer = this.removeSafeTimer.bind(this);
this.handleJobCompletion = this.handleJobCompletion.bind(this);

this.validateJob = validateJob;
this.getName = getName;
Expand Down Expand Up @@ -351,8 +356,7 @@ class Bree extends EventEmitter {
this.workers[name].terminate();
delete this.workers[name];

// remove closeWorkerAfterMs if exist
this.removeSafeTimer('closeWorkerAfterMs', name);
this.handleJobCompletion(name);

this.emit('worker deleted', name);
}
Expand Down Expand Up @@ -405,8 +409,7 @@ class Bree extends EventEmitter {

delete this.workers[name];

// remove closeWorkerAfterMs if exist
this.removeSafeTimer('closeWorkerAfterMs', name);
this.handleJobCompletion(name);

this.emit('worker deleted', name);
});
Expand Down Expand Up @@ -638,6 +641,19 @@ class Bree extends EventEmitter {
createWorker(filename, options) {
return new Worker(filename, options);
}

handleJobCompletion(name) {
// remove closeWorkerAfterMs if exist
this.removeSafeTimer('closeWorkerAfterMs', name);

if (
this.config.removeCompleted &&
!this.timeouts.name &&
!this.intervals.name
) {
this.config.jobs = this.config.jobs.filter((j) => j.name !== name);
}
}
}

// plugins inspired by Dayjs
Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const path = require('path');
const { once } = require('events');

const test = require('ava');

const delay = require('delay');
const humanInterval = require('human-interval');
const FakeTimers = require('@sinonjs/fake-timers');
Expand Down Expand Up @@ -298,3 +301,17 @@ test('sets logger to noop if set to false', (t) => {
t.true(typeof bree.config.logger.warn === 'function');
t.true(typeof bree.config.logger.error === 'function');
});

test('removes job on completion when config.removeCompleted is `true`', async (t) => {
const bree = new Bree({
jobs: ['basic'],
...baseConfig,
logger: false,
removeCompleted: true
});

bree.run('basic');
await once(bree.workers.basic, 'exit');

t.is(bree.config.jobs.length, 0);
});
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ declare class Bree extends EventEmitter {

createWorker: (filename: string, options: Partial<WorkerOptions>) => Worker;

handleJobCompletion: (name: string) => void;

constructor(config?: Bree.BreeOptions);
}

Expand All @@ -76,6 +78,7 @@ declare namespace Bree {
root: string | boolean;
silenceRootCheckError: boolean;
doRootCheck: boolean;
removeCompleted: boolean;
timeout: number | boolean;
interval: number;
timezone: string;
Expand Down

0 comments on commit 139a949

Please sign in to comment.