如何使用 Node-Schedule 运行 Cron Task Once
How to Run Cron Task Once with Node-Schedule
我使用 node-schedule 来安排一个任务在特定时间只执行一次。文档显示对我来说:
Date-based Scheduling: Say you very specifically want a function to
execute at 5:30am on December 21, 2012. Remember - in JavaScript - 0 -
January, 11 - December.
const schedule = require('node-schedule');
const date = new Date(2012, 11, 21, 5, 30, 0);
const job = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
});
但是这段代码 运行 每分钟都会执行一次任务,我只希望它 运行 在此处指定的特定时间只执行一次任务:
const date = new Date(2012, 11, 21, 5, 30, 0);
这里的日期是默认的。我在我的代码中将其更改为未来日期,如下所示:
const now = new Date().getTime();
const seconds = 180;
const parsedDate = new Date(Date.parse(now));
const date = new Date(parsedDate.getTime() + (1000 * seconds));
schedule.scheduleJob(date, function(data) {
console.log("Job ran @", new Date().toString());
});
在这里,我只想在从当前日期算起 180 秒(3 分钟)后执行一项作业。但控制台结果显示控制台日志在部署后每分钟写入一次:
> Job ran @ Tue Mar 16 2021 08:56:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:57:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:58:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:59:00 GMT+0000 (GMT)
感谢您的帮助。
我通过在我的代码中添加时区来解决这个问题。我使用另一个 post 的 answer。这里的功能是在日期功能中添加时区功能,方便快捷。
This should solve your problem, please feel free to offer fixes. This
method will account also for daylight saving time for the given date.
Date dateWithTimeZone = (timeZone, year, month, day, hour, minute, second) => {
let date = new Date(Date.UTC(year, month, day, hour, minute, second));
let utcDate = new Date(date.toLocaleString('en-US', { timeZone: "UTC" }));
let tzDate = new Date(date.toLocaleString('en-US', { timeZone: timeZone }));
let offset = utcDate.getTime() - tzDate.getTime();
date.setTime( date.getTime() + offset );
return date;
};
然后我的代码看起来像这样:
schedule.scheduleJob(dateWithTimeZone("Europe/Istanbul",
req.body.year, req.body.month, req.body.day, req.body.hour,
req.body.minute, req.body.second), function(data) {
console.log("Job ran @", new Date().toString());
});
最后它对我来说很好用。我可以安排在我喜欢的时区内只执行一次作业。
感谢您的关注。关于...
我找到了一种安排 cron 作业的简单方法。
const schedule = require('node-schedule');
const moment = require('moment');
const rule = scheduled.RecurrenceRule();
let time = '2021-07-30 14:20:00';
rule.tz = 'Asia/Kolkata';
rule.year = moment(time).year();
rule.month = moment(time).month();
rule.date = moment(time).date();
rule.hour = moment(time).hours();
rule.minute = moment(time).minutes();
rule.second = moment(time).seconds();
schedule.scheduleJob(rule, async function () {
console.log("Scheduled")
});
我使用 node-schedule 来安排一个任务在特定时间只执行一次。文档显示对我来说:
Date-based Scheduling: Say you very specifically want a function to execute at 5:30am on December 21, 2012. Remember - in JavaScript - 0 - January, 11 - December.
const schedule = require('node-schedule'); const date = new Date(2012, 11, 21, 5, 30, 0); const job = schedule.scheduleJob(date, function(){ console.log('The world is going to end today.'); });
但是这段代码 运行 每分钟都会执行一次任务,我只希望它 运行 在此处指定的特定时间只执行一次任务:
const date = new Date(2012, 11, 21, 5, 30, 0);
这里的日期是默认的。我在我的代码中将其更改为未来日期,如下所示:
const now = new Date().getTime();
const seconds = 180;
const parsedDate = new Date(Date.parse(now));
const date = new Date(parsedDate.getTime() + (1000 * seconds));
schedule.scheduleJob(date, function(data) {
console.log("Job ran @", new Date().toString());
});
在这里,我只想在从当前日期算起 180 秒(3 分钟)后执行一项作业。但控制台结果显示控制台日志在部署后每分钟写入一次:
> Job ran @ Tue Mar 16 2021 08:56:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:57:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:58:00 GMT+0000 (GMT)
> Job ran @ Tue Mar 16 2021 08:59:00 GMT+0000 (GMT)
感谢您的帮助。
我通过在我的代码中添加时区来解决这个问题。我使用另一个 post 的 answer。这里的功能是在日期功能中添加时区功能,方便快捷。
This should solve your problem, please feel free to offer fixes. This method will account also for daylight saving time for the given date.
Date dateWithTimeZone = (timeZone, year, month, day, hour, minute, second) => { let date = new Date(Date.UTC(year, month, day, hour, minute, second)); let utcDate = new Date(date.toLocaleString('en-US', { timeZone: "UTC" })); let tzDate = new Date(date.toLocaleString('en-US', { timeZone: timeZone })); let offset = utcDate.getTime() - tzDate.getTime(); date.setTime( date.getTime() + offset ); return date; };
然后我的代码看起来像这样:
schedule.scheduleJob(dateWithTimeZone("Europe/Istanbul",
req.body.year, req.body.month, req.body.day, req.body.hour,
req.body.minute, req.body.second), function(data) {
console.log("Job ran @", new Date().toString());
});
最后它对我来说很好用。我可以安排在我喜欢的时区内只执行一次作业。
感谢您的关注。关于...
我找到了一种安排 cron 作业的简单方法。
const schedule = require('node-schedule');
const moment = require('moment');
const rule = scheduled.RecurrenceRule();
let time = '2021-07-30 14:20:00';
rule.tz = 'Asia/Kolkata';
rule.year = moment(time).year();
rule.month = moment(time).month();
rule.date = moment(time).date();
rule.hour = moment(time).hours();
rule.minute = moment(time).minutes();
rule.second = moment(time).seconds();
schedule.scheduleJob(rule, async function () {
console.log("Scheduled")
});