Your app using node-cron could be on a delayed fuse

      No Comments on Your app using node-cron could be on a delayed fuse

Node cron (npm i node-cron)is an excellent library and it serves it purpose quite well unless you decide to use Time Zone dependent cron, then this library might expose you to a delayed bug as the code will work right 6 months a year.

The CODE

If you want to use timezone with node cron your code might look similar to the one given below

cron.schedule('46 00 * * *',() => {
   //code to be executed
  console.log("Tik")
  },{
    scheduled: true,
    timezone: "America/New_York"
  });

The above cron is scheduled to fire every 12:45 am America/New_York time.

The BUG

In the above code we have specifically configured our cron to fire only when it is 12:45 am in New York. This code will work fine for 6 months only and for other half of year it will execute 1 hr late 1:45 am.

The CAUSE

This cause of the bug is very fundamental. This library is not communicating using any API to get DST data. This library is entirely dependent upon tz-offset to calculate time and does not consider Daylight Saving Time. Making this library fundamentally flawed when it comes to timezone.

The Solution

you can switch to cron library npm i cron it uses luxon. and it is very similar to use. Above code while using cron will look something like this

const CronJob = require('cron').CronJob;
const job = new CronJob('46 00 * * *', () => {
    console.log('Tik');
}, null, true, 'America/New_York');
job.start();

The CONCLUSION

It would be easy to fall in the trap of criticizing the creator then to create a pull request with solution. At most, the creator can be called upon to mention it somewhere in readme.

A pat on the back !!