2015-06-21 73 views
1

我正在使用CronJob。如果我执行myFunction的时候得到一个错误,我应该怎么办CronJob执行功能中的处理错误

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. */ 
); 
+0

这取决于。什么样的错误? –

+0

查询数据库时出错 –

回答

0

只要把如果错误/ else语句。这样你的应用程序不会崩溃。您可以将记录器添加到您的应用程序并记录在cron作业中发生的所有错误。

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. */ 
);