Name: bedrock-jobs
Owner: Digital Bazaar, Inc.
Description: Bedrock jobs
Created: 2015-01-13 23:35:56.0
Updated: 2016-03-03 14:31:05.0
Pushed: 2017-03-01 04:45:19.0
Homepage: null
Size: 50
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A bedrock module that exposes an API for scheduling and executing background jobs.
install bedrock-jobs
s
bedrock = require('bedrock');
ire('bedrock-mongodb');
scheduler = require('bedrock-jobs');
ock.config.scheduler.jobs.push({
: 'myproject.jobs.Scan',
pe: 'myproject.jobs.Scan',
repeat forever, run every minute
hedule: 'R/PT1M',
no special priority
iority: 0,
no concurrency limit, every bedrock worker can run one of these jobs
ncurrency: -1
ock.events.on('bedrock.init', function() {
define job type so it will run in this bedrock instance
heduler.define('myproject.jobs.Scan', function(job, callback) {
doSomeKindOfScan(function(err) {
// scan finished
callback(err);
});
;
Jobs can be automatically scheduled by specifying job
objects in
bedrock.config.scheduler.jobs
.
For more documentation on configuration, see config.js.
Defines a new job type. Schedules jobs will only be processed if their types
have been defined. A job type
(a unique string) specifies:
Note that, due to current implementation limitations, lock duration can only be configured on a per-job-type basis, it cannot be configured per-job.
The options
may include:
-1
for unlimited.Any options.defaults
given will override the defaults specified by
bedrock.config.scheduler.defaults
.
The fn
parameter is a function to call to execute the job. It takes a job
object and a callback
as parameters. Once the job completes (or an error
occurs that causes it to stop), the callback
must be called. If an error
occurs, it should be passed to the callback
.
Creates a new job ID. The callback
will be called once the ID is ready or
if an error occurs. The first parameter will be an error or null
and the
second will be the ID.
Schedules a new job. The job
must have a type set (job.type
). It may
specify a unique id, if it does not, one will be generated. It may also specify
how often to run the job and any job-specific data. The job
will be passed
to the function that was previously passed to define
to define its type.
Jobs may be scheduled that do not have defined types, however, they will never be executed by this particular scheduler (other schedulers may execute them). This allows distributed systems that share a common database to be configured such that some machines will execute certain types of jobs that are found in the database whilst others will not. For example, one machine may only run jobs of type A and one machine may only run jobs of type B, but both may be capable of scheduling either type of job.
The options
may include:
true
to run the job immediately if its schedule permits.The callback
will be called once the scheduling operation completes or if
an error occurs. If an error occurred, it will be passed to the callback
.
Unschedules a job or all jobs of a certain type. The options
parameter must
be given any have either:
The callback
will be called once the operation completes. If an error occurs,
it will be passed to the callback
.