0

I'm using Cron; a nodejs package for cron job handling in NodeJs. Here's how I'm running a cron job:

var job = new CronJob({
  cronTime: '00 30 11 * * 1-5',
  onTick: function() {
    /*
     * Runs every weekday (Monday through Friday)
     * at 11:30:00 AM. It does not run on Saturday
     * or Sunday.
     */
  }
});
job.start();

It's running flawlessly but is there any standard way to handle exception dates array handling? For example here's my dates array of national holidays and I don't want to run my cron job on these days:

['28-01-2017', '1-05-2017', '14-08-2016', '15-09-2016', '16-09-2016']
Adil
  • 21,278
  • 7
  • 27
  • 54

1 Answers1

3

You can not add exclusions to your cron job. You are much better off adding to your code the logic to not run on those days.

var job = new CronJob({
  cronTime: '00 30 11 * * 1-5',
  onTick: function() {
    var exclude = ['28-01-2017', '1-05-2017', '14-08-2016', '15-09-2016', '16-09-2016']
    if (exclude.indexOf(convertDate()) > -1) {
        console.log('dont run');
    } else {
        console.log('run');
    }
  }
});
job.start();

function convertDate() {
    var d = new Date();
    return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('-');
}

function pad(s) {
    return (s < 10) ? '0' + s : s;
}
jjbskir
  • 8,474
  • 9
  • 40
  • 53
  • Thanks, this is what I'm currently doing but if there's really not any standard way of handling those exception dates array then that should be the accepted answer. – Adil Jan 17 '17 at 04:07