我正在使用node-cron在作业运行时触发一个函数(onTick
)并在作业停止时触发一个函数(onComplete
);但是,我没有得到预期的行为。
如果作业实际运行,则onComplete
永远不会运行:
var CronJob = require('cron').CronJob;
var someDate = new Date('July 11, 2016 20:44:20');
var timeZone = 'America/Los_Angeles'
var job = new CronJob(someDate, function() {
/* runs once at the specified date. */
console.log("job running")
}, function() {
/* This function is executed when the job stops */
console.log("job stopping")
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);
输出:
hello-node-cron ❯ node date-usage.js
job running
但奇怪的是,如果日期已经过去,那么onComplete
将会运行:
输出:
hello-node-cron ❯ node date-usage.js
job stopping
我在这里错过了什么吗?
答案 0 :(得分:0)
除了直觉,根据其one测试用例,onComplete
是通过设计调用stop
API触发的。
var job = new CronJob(someDate, function() {
/* runs once at the specified date. */
console.log("job running")
this.stop() // <===================================== Add stop here.
}, function() {
/* This function is executed when the job stops */
console.log("job stopping")
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);