【问题标题】:How to Run Cron Task Once with Node-Schedule如何使用 Node-Schedule 运行一次 Cron 任务
【发布时间】:2023-03-03 12:15:01
【问题描述】:

我使用node-schedule 来安排一个任务在特定时间只执行一次。文档显示对我来说:

基于日期的调度:假设您非常特别想要一个函数 在 2012 年 12 月 21 日凌晨 5:30 执行。记住 - 在 JavaScript 中 - 0 - 1 月 11 日 - 12 月。

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 分钟)后 cron 执行一项工作。但是控制台结果显示控制台日志在部署后每分钟写入一次:

>  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)

感谢您的帮助。

【问题讨论】:

    标签: node.js date server cron node-schedule


    【解决方案1】:

    我通过在我的代码中添加时区来解决这个问题。我使用另一个帖子中的answer。这里的功能是在日期功能中添加时区功能,非常简单。

    这应该可以解决您的问题,请随时提供修复。这 方法还将考虑给定日期的夏令时。

      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());
    });
    

    最后它对我来说很好用。我只能在我喜欢的时区安排作业执行一次。

    感谢您的关注。关于……

    【讨论】:

      【解决方案2】:

      我找到了一种安排 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")
      });
      

      【讨论】:

      • 在哪里声明了“预定”变量?
      猜你喜欢
      • 1970-01-01
      • 2020-09-27
      • 2014-08-07
      • 2021-05-14
      • 1970-01-01
      • 2010-12-02
      • 2019-10-09
      • 2014-12-21
      • 2015-07-29
      相关资源
      最近更新 更多