sails-hook-cron
Sails hook for running cron tasks.
Getting Started
Install it via npm:
npm install sails-hook-cron
Configure config/cron.js
in your project:
moduleexportscron = myFirstJob: schedule: '* * * * * *' { console; console; } ;
Examples
Schedule field syntax is:
// ['seconds', 'minutes', 'hours', 'dayOfMonth', 'month', 'dayOfWeek'] moduleexportscron = firstJob: schedule: '30 47 15 17 may *' // in May 17 15:47:30 GMT-0300 (BRT) { console; } timezone: 'America/Sao_Paulo' // timezone Brazil example ;
You can define cron tasks only with required fields:
moduleexportscron = firstJob: schedule: '* * * * * *' { console; } secondJob: schedule: '*/5 * * * * *' { console; } ;
You can define advanced fields:
moduleexportscron = myJob: schedule: '* * * * * *' { console; } { console; } start: true // Start task immediately timezone: 'Ukraine/Kiev' // Custom timezone context: undefined // Custom context for onTick callback runOnInit: true // Will fire your onTick function as soon as the request initialization has happened. ;
You can get created jobs and start\stop them when you wish:
// config/cron.jsmoduleexportscron = myJob: schedule: '* * * * * *' { console; } start: false ; // api/controllers/SomeController.jsmoduleexports = { sailshookscronjobsmyJobstart; sailshookscronjobsmyJob; };
Context
There are three states for the context, i.e. this on onTick
call:
- When you don’t declare context -
this
points to the Sails object. - If you declare it as a null (
context: null
),this
points to the original context from the cron library. - Otherwise, if you declare a context with some object (
context: {foo: 'bar'}
),this
will point to the object instead.