OnComplete () not called in node cron
Hi I am trying to check node-cron
but I cannot get the desired answer in one case.
I need to initiate the cron request again when the current cron exits. So, I need onComplete()
to call, but I can't get the callback.
My code snippet:
CronWrapper.prototype.pushNotificationCron = function() {
// change console to winston in real implementation.
console.log('Creating job');
var jobPattern = '*/10 * * * * *';
var job = new CronJob(jobPattern, onJobStarted, onJobCompleted, false);
console.log('Starting job');
job.start();
};
var onJobStarted = function(){
var date = new Date();
console.log('Cron started on \t' + date);
return;
};
var onJobCompleted = function(){
winston.info('Job completed:');
};
Output:
Cron started on Tue Dec 16 2014 12:59:40 GMT+0530 (IST)
Cron started on Tue Dec 16 2014 12:59:50 GMT+0530 (IST)
Cron started on Tue Dec 16 2014 13:00:00 GMT+0530 (IST)
Please indicate what mistake I am making.
Lib info:
"cron": "1.0.5"
source to share
After more detailed documentation, I found that I was missing the stop () call.
As stated in the documentation
onComplete - [OPTIONAL] - a function that is triggered when the task is completed , when it is stopped .
So now I manually call stop()
on completion and get the callback. So now my code looks like this:
CronWrapper.prototype.pushNotificationCron = function() {
// change console to winston in real implementation.
console.log('Creating job');
var jobPattern = '*/10 * * * * *';
var job = new CronJob(jobPattern, function(){
var date = new Date();
console.log('Cron started on \t' + date);
job.stop();
}, onJobCompleted, false);
console.log('Starting job');
job.start();
};
// var onJobStarted = function(){
// var date = new Date();
// console.log('Cron started on \t' + date);
// };
var onJobCompleted = function(){
winston.info('Job completed:');
};
Output
Cron started on Tue Dec 16 2014 13:13:30 GMT+0530 (IST)
info: Job completed:
source to share