shamon shamsudeen
shamon shamsudeen

Reputation: 5858

cron job node.js: job is repeating infinitely

Trying to achive cron-jobs in my node.js application using this package.

https://www.npmjs.com/package/node-schedule

here i am running a cronjob on a specific date and time.

schedule.scheduleJob(new Date(date), function(){
                        console.log('The world is going to end today.');
                    });

From their doc

Say you very specifically want a function to execute at 5:30am on December 21, 2012. Remember - in JavaScript - 0 - January, 11 - December. the date should be look like this
var date = new Date(2012, 11, 21, 5, 30, 0);

and i am generating cron job date from a unix timestamp using moment.

   let date = moment.unix(dateInUTC).format('YYYY-M-DD-H-mm-s').split('-');
   date[1]  = date[1]-1;
   return date.toString();

sample output of the above date

UNIX  time 1516196220
Converted date 2018,0,17,19,07,0

everything works fine but the cron job runs infinitely ie my console look like this

 console.log('The world is going to end today.');
 console.log('The world is going to end today.');
 console.log('The world is going to end today.');
 console.log('The world is going to end today.');
 -------------------------------
-------------------------------
 endless

Upvotes: 1

Views: 1009

Answers (1)

slugonamission
slugonamission

Reputation: 9642

Your date won't end up in the correct format:

let date = moment.unix(dateInUTC).format('YYYY-M-DD-H-mm-s').split('-');
date[1]  = date[1]-1;
return date.toString();

So, date is an array, and you're calling .toString(). This will end up with something like the following:

'10,20,30,40,50'

This isn't in a parseable form by the Date constructor, which expects it in ISO8601 format.

moment is already emitting a Date value for the timestamp that you specified; can you just use that directly?

Upvotes: 1

Related Questions