This module is a plugin for the Seneca framework. It provides scheduling capability for actions.
With this module you can:
The default implementation uses the node-schedule module to handle the event scheduling
You can customize this by overriding the appropriate actions.
If you're using this module, feel free to contact me on twitter if you have any questions! :) @davidmarkclem
Current Version: 0.0.0
Tested on: node 0.10.26, seneca 0.5.15
Schedule a single future event:
var seneca = require('seneca')();
seneca.use('scheduler');
seneca.ready(function(err){
if( err ) return console.log(err);
seneca.act({
role:'scheduler',
cmd:'register',
for: '22/10/15 17:24:02',
task: function () {
//do things
console.log('doing things');
},
})
})
The for argument can also be a Date object or an object literal, as well as being a string.
Schedule a recurring event:
var seneca = require('seneca')();
seneca.use('scheduler');
seneca.ready(function(err){
if( err ) return console.log(err);
seneca.act({
role:'scheduler',
cmd:'register',
every: {
minute: 30,
hour: 4,
day: 5
},
task: function () {
//do things
console.log('doing things');
},
})
})
This will schedule an event for the 30th minute, of the 4th hour of 5th day of the month (for days of the week we use dayOfWeek).
There's also an alternative way to express recurrence:
var seneca = require('seneca')();
seneca.use('scheduler');
seneca.ready(function(err){
if( err ) return console.log(err);
seneca.act({
role:'scheduler',
cmd:'register',
every: {
'30th': 'minute',
'4th': 'hour',
'5th': 'day'
},
task: function () {
//do things
console.log('doing things');
},
})
})
npm install seneca
npm install seneca-scheduler
You'll need the seneca module to use this module - it's just a plugin.
To load the plugin:
seneca.use('scheduler', { ... options ... })
To isolate logging output from the plugin, use:
node your-app.js --seneca.log=plugin:scheduler
For more logging options, see the Seneca logging tutorial. You may, for example, wish to log task output to a separate file for audit purposes.
All actions provide results via the standard callback format: function(error,data){ ... }
.
Register a task with the scheduler
Object with properties:
None.
None.
Outputs an array of all the job id's currently scheduled
None.
Array containing ID strings.
None.
None.
Returns a task object when passed an id.
id: The id of the task to fetch
Object with properties:
None.
None.
Removes a task from the scheduler.
id: The id of the task to remove. Can also be an array of id's to remove. ids: An array of id's to remove.
Nothing.
None.
None.
Clears all scheduled tasks
None.
Nothing
None.
None.
To see what this plugin is doing, try:
node your-app.js --seneca.log=plugin:scheduler
This will print action logs and plugin logs for the user plugin. To skip the action logs, use:
node your-app.js --seneca.log=type:plugin,plugin:scheduler
You can also set up the logging programmatically:
var seneca = require('seneca')({
log:{
map:[
{plugin:'scheduler',handler:'print'}
]
}
})
For more on logging, see the seneca logging example.
Run tests with:
npm test
If debugging tests:
npm run debug-test
Then go to http://localhost:8080/debug?port=5858 in Chrome to use dev-tools to debug. Awesome sauce.