Handling error in CronJob executing function

3.5k views Asked by At

I am using CronJob. What should I do if I get an error when executing myFunction

var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function myFunction() {
     //Do something
  }, function () {
    /* This function is executed when the job stops */
  },
  true, /* Start the job right now */
  timeZone /* Time zone of this job. */
);
1

There are 1 answers

0
Marko On

Just put if/else statement for error. That way your app won't crash. You can add a logger to your app and log all errors that occur in cron job.

var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function myFunction() {
  User.findOne({_id: user_id}, function(err, results) {
    if (err) {
       console.log(err);
    } else {
       //success
       console.log(results);
    }
  })
  }, function () {
    /* This function is executed when the job stops */
  },
  true, /* Start the job right now */
  timeZone /* Time zone of this job. */
);