我需要一个在 node.js 中制作的网络应用程序的作业调度程序。
我看过 node-cron 和 node-scheduler,但 booth 似乎是建立在 Node 的 setTimeout()
函数之上的。问题在于 setTimeout()
的最大超时大约为 25 天,如下所述:
http://nodejs.org/api/globals.html#globals_settimeout_cb_ms
如果我需要安排时间,比如每三个月一次...我该如何解决?
最佳答案
查看 node-cron
中的这一行:https://github.com/ncb000gt/node-cron/blob/master/lib/cron.js#L382
它基本上表明 node-cron 处理了 setTimeout()
溢出并将较大的时间跨度分成多个较小的时间跨度。您应该能够安全地使用它。
其实这个功能is covered by node-cron's unit测试也是如此。如果提供给 setTimeout()
的值大于最大值,它将立即触发。此单元测试检查在提供巨大的值(1000 个月)后,测试是否不会在 250 毫秒的时间窗口内触发。
'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);
},
关于javascript - 工作时间超过 25 天的 Node 的调度程序。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25326147/