controlled-schedule
v2.0.0
Published
Recurring schedule for async tasks. Schedule next execution only after the current one finishes
Downloads
7
Maintainers
Readme
Controlled schedule
Recurring schedule for async tasks. Schedule next execution only after the current one finishes.
e.g:
const execute = require('controlled-schedule');
function task() {
// some async operation
// should accept a callback as parameter
// or return a Promise
}
execute(task)
.every('20s')
.startIn('1m')
.stopAfter('2h')
.on('error', function(err) {
// log error
})
.on('stop', function() {
// do something
});
For more examples check /examples
folder and the end of this README.
Instalation
npm install controlled-schedule --save
Usage
Your task must return a
Promise
or accept acallback(err, value)
as argument.Methods accept time in milliseconds or in string format. e.g. '10 days', '10d', '10h', '2.5 hrs', '10h', '10m', '10s'
Every method returns the object reference so you can chain all operations.
Usage:
const execute = require('controlled-schedule');
//create schedule object
let schedule = execute(task);
// define interval between each execution
schedule.every('20s');
// define how long to start the first execution
schedule.startIn('1m');
// start immediately
schedule.start();
// define how long the task will be rescheduled
schedule.stopAfter('2h');
// stop the schedule (waits for task to finish if it's executing)
schedule.stop();
// events
// after a task runs
// err - error object passed by the task
// value - value returned by the task
schedule.on('run', function(err, value) {
});
// after a task runs with success
// value - value returned by the task
schedule.on('success', function(value) {
});
// after a task runs and return an error
// err - error object returned by the task
schedule.on('error', function(err) {
});
// when schedule stop is triggered
schedule.on('stop', function() {
});
// return Promise for the next execution
schedule.nextRun()
.then(function(value) {
})
.catch(function(err) {
});
More examples
Run task indefinitely with 1 hour interval between executions:
execute(task)
.every('1h')
.start();
Run for 1 minute starting every next task immediately after previous one finishes:
execute(task)
.stopAfter('1m')
.start();
Run task with 1 minute interval and stop if an error occurred:
let schedule =
execute(task)
.every('1m')
.on('error', function(err) {
schedule.stop();
console.log(err);
})
.start()
For more examples check /examples
folder.
License
MIT