我想在特定时间在nodejs应用中执行任务。
因此,我使用Timer编写了如下所示的源代码。
var _to_execute_time = 1571221163000; //The timestamp to execute the task.
var _current_timestamp = Date.now(); //The current timestamp.
//Scheduling the task using setTimeout();
setTimeout(task_function, _to_execute_time - _current_timestamp);
问题是,如果系统在执行任务之前重新启动,则setTimeout()将被取消。
如何解决此问题并可靠地运行任务?
您可以使用node-cron
。烤架将在指定的时间运行。
您可以使用的npm软件包是node-cron
。
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
要么
cron.schedule('0 11 * * *', () => {
console.log('running a task at 11:00 AM');
});
允许设置时间的字段:
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句