Skip to content

Commit

Permalink
added defaultCompletion configure option. Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Morison authored and Chris Morison committed Mar 27, 2021
1 parent 184a105 commit 46ae076
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,26 @@ The configuration object supports `date`, `in`, `on`, and `priority`, all of whi

```javascript
Jobs.configure({
maxWait: Number, // (milliseconds) specify how long the server could be inactive before another server takes on the master role (default = 5min)
startupDelay: Number, // (milliseconds) specify how long after server startup the package should start running
setServerId: String || Function, // determine how to set the serverId - see below. (default = random string)
log: Boolean || Function, // determine if/how to log the package outputs (defalt = console.log)
autoStart: Boolean, // specify if all job queues should start automatically on first launch (default = true)...
// ... after server relaunch the list of paused queues is restored from the database.
// (milliseconds) specify how long the server could be inactive before another server
// takes on the master role (default = 5min)
maxWait: Number,

// (milliseconds) specify how long after server startup the package should start running
startupDelay: Number,

// determine how to set the serverId - see below. (default = random string)
setServerId: String || Function,

// determine if/how to log the package outputs (defalt = console.log)
log: Boolean || Function,

// specify if all job queues should start automatically on first launch (default = true)...
// ... after server relaunch the list of paused queues is restored from the database.
autoStart: Boolean,

// whether to mark successfull just as successful, or remove them,
// otherwise you have to resolve every job with this.succesS() or this.remove()
defaultCompletion: 'success' | 'remove',
})
```
`setServerId` - In a **multi-server deployment**, jobs are only executed on one server. Each server should have a unique ID so that it knows if it is control of the job queue or not. You can provide a function which returns a serverId from somewhere (e.g. from an environment variable) or just use the default of a random string. In a **single-server deployment** set this to a static string so that the server knows that it is always in control and can take control more quickly after a reboot.
Expand Down Expand Up @@ -401,6 +415,9 @@ If any of these differences make this package unsuitable for you, please let me

## Version History

#### 1.0.11 (2021-03-28)
- Added the `'defaultCompletion'` [Jobs.configure](#jobsconfigure) option. Suggested in #10.

#### 1.0.10 (2021-02-17)
- Better support for [Async Jobs/Promises](#async-jobs). Fixes #7.
- While jobs are executing their status is set to `'executing'`.
Expand Down
24 changes: 17 additions & 7 deletions jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export namespace Jobs {
log: typeof console.log | boolean;
autoStart: boolean;
setServerId?: string | Function;
defaultCompletion?: 'success' | 'remove';
}

export interface JobConfig {
Expand Down Expand Up @@ -72,6 +73,7 @@ export namespace Jobs {
setServerId: Match.Maybe(Match.OneOf(String, Function)),
log: Match.Maybe(Match.OneOf(undefined, null, Boolean, Function)),
autoStart: Match.Maybe(Boolean),
defaultCompletion: Match.Maybe(Match.Where((val => /^(success|remove)$/.test(val)))),
});
Object.assign(settings, config);
if (settings.log === true) settings.log = console.log;
Expand Down Expand Up @@ -457,6 +459,18 @@ export namespace Jobs {
};

let isAsync = false;
function completed() {
if (!action) {
if (settings.defaultCompletion == 'success') {
setJobState(job._id, 'success');
} else if (settings.defaultCompletion == 'remove') {
remove(job._id);
} else {
console.warn('Jobs', "Job was not resolved with success, failure, reschedule or remove. Consider using the 'defaultCompletion' option.", job);
setJobState(job._id, 'failure');
}
}
}

try {
setJobState(job._id, 'executing');
Expand All @@ -465,10 +479,7 @@ export namespace Jobs {
isAsync = true;
res.then(() => {
log('Jobs', ' Done async job', job.name, 'result='+action);
if (!action) {
console.warn('Jobs', 'Async Job was not resolved with success, failure, reschedule or remove', job);
setJobState(job._id, 'failure');
}
completed();
});
} else {
log('Jobs', ' Done job', job.name, 'result='+action);
Expand All @@ -479,9 +490,8 @@ export namespace Jobs {
if (action != 'reschedule') self.failure();
}

if (!isAsync && !action) {
console.warn('Jobs', 'Job was not resolved with success, failure, reschedule or remove', job);
setJobState(job._id, 'failure');
if (!isAsync) {
completed();
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: 'wildhart:jobs',
version: '1.0.10',
version: '1.0.11',
summary: 'Schedule jobs to run at a later time, including multi-server, super efficient',
git: 'https://github.com/wildhart/meteor.jobs',
documentation: 'README.md'
Expand Down
1 change: 1 addition & 0 deletions wildhart-jobs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare module 'meteor/wildhart:jobs' {
due?: Date;
state: string;
callback?: Function;
defaultCompletion?: 'success' | 'remove';
}

export type JobStatus = "pending" | "success" | "failure" | "executing";
Expand Down

0 comments on commit 46ae076

Please sign in to comment.