Workers module

The module uses monq to configure and launch distributed jobs and cron to schedule those jobs.

Usage

Configure the job in the workers key:

"workers": [
  {
    "worker": "unreserveExpired",
    "handler": "./jobs/unreserveExpired",
    "when": "0 */1 * * * *"
  }]

If you add the when key, the service will use the cron npm module to schedule the job. The time zone used is always UTC.

Configure the job in the application:

module.exports = (base) => {
  return (params, done) => {
    if (params.since) {
        // Do the work
       ...
        // Call the callback when done.
       done();
    } else {
        // Call the callback if there is an errror.
       done(new Error('error_code'));
    }
    done();
  }
}

Manualy launch a job

To manually launch a job (enqueue it for execution) use the enqueue method:

base.workers.enqueue('unreserveExpired', {since: '2016-06-20T21:20:11.287Z'});

This service is best used with the events module. Listen to an event and launch a job:

base.events.listen(productsChannel, (msg) => {
  base.workers.enqueue('indexProduct', msg);
});

Local jobs

If you want to execute a local job (not distributed), use the scheduleLocalJob method:

base.workers.scheduleLocalJob({
  name: 'MyCheck',
  when: '*/5 * * * * *',
  worker: () => {
    base.bus.publish('MyChannel', { ts: new Date() });
  }
});