Hapi and MongoDB powered job queue.
npm install hapi-job-queue --save
var server = new Hapi.Server();
var mongoUrl = 'mongodb://localhost:27017/something';
server.connection({port: 3000});
server.method('emailUsers', function(data, done) {
server.plugins.emailService.spam(data.group, done);
});
server.register([
{ register: require('hapi-job-queue'), options: {
connectionUrl: mongoUrl,
endpoint: '',
jobs: [
{
name: 'email-users',
enabled: true,
schedule: 'on the last day of the month',
method: 'emailUsers', //server method
tasks: [ // each task will run 'emailUsers' with the task as the data property
{
group: 'pendingUsers'
},
{
group: 'approvedUsers'
},
{
group: 'newsletterUsers'
}
]
}
]
} }
], function() {
// server start etc...
});
connectionUrl
- mongodb connection urlendpoint
- Path for api endpoint. Set to false to disable. No trailing slash. (default: false)auth
- Auth strategy to use for api endpoints. (default: false)concurrentTasks
- Number of instances of method
that can run simultaneously. Note: This is limited on a per job basis. Two jobs running at the same time will each have a max of concurrentTasks
. (default: 5)collection
- DB collection to use. (default: Jobs)verbose
- Extra logging. Tagged hapi-job-queue, info
. (default: true)jobs
- Array of job objects.
name
- Name of the job. Used in api endpoints and methods.enabled
- Enables or disables a job.single
- Set to true if this job will be manually run and not on a timer. (default false)schedule
- (optional) Later style time definition.cron
- (optional) Later style cron definition.cronSeconds
- (optional) Use if the above cron setting is in seconds.method
- Method to run for each task or once when no tasks are assigned. Can be a hapi server method or a function. function(data, callback)
tasks
- (optional) Array of data to be passed to job method. Each item in the array will spawn an instance of method
.These methods can be found in server.plugins.jobs
.
addJob
- params: job
, callback(err)
- Adds a job. Uses same job format as options.getJobs
- params: callback(err)
- Returns all jobsenableJob
- params: jobName
, callback(err)
- Enables a job.disableJob
- params: jobName
, callback(err)
- Disables a job.runSingle
- params: jobName
, tasks
, callback(err)
- Runs a single job. Tasks uses the same task format in options.If you enable the web api these endpoints will be exposed.
GET /
- Returns all jobs.GET /enable/{jobName}
- Enables a jobGET /disable/{jobName}
- Disables a jobPOST /run/{jobName}
- Runs a job. Accepts a json payload of task data.