I need a job scheduler for a web app made in node.
I have looked at both node-cron and node-scheduler, but booth seems to be built on top of Node's setTimeout()
function. The problem with this is that setTimeout()
has a max timeout of roughly 25 days, as stated here:
http://nodejs.org/api/globals.html#globals_settimeout_cb_ms
If I need scheduling, lets say every third month... how do I solve this?
Look at this line in node-cron
: https://github.com/ncb000gt/node-cron/blob/master/lib/cron.js#L382
It basically indicates that node-cron handles the setTimeout()
overflow and will split the larger timespan in multiple smaller ones. You should be able to use it safely.
In fact, this functionality is covered by node-cron's unit tests as well. If the value supplied to the setTimeout()
is greater than the maximum, it will fire immediately. This unit tests checks whether, after supplying the huge value (1000 months), the test will not fire in the 250 ms time window.
'test long wait should not fire immediately': function(assert) {
assert.expect(1);
var count = 0;
var d = new Date().getTime() + 31 * 86400 * 1000;
var job = cron.job(new Date(d), function() {
assert.ok(false);
});
job.start();
setTimeout(function() {
job.stop();
assert.ok(true);
assert.done();
}, 250);
},
agenda will also handle your needs. It stores schedule job times in a database and then will poll the database on a given interval (agenda.processEvery
) looking for jobs that need to be done.
Full disclosure, I am the author of agenda.