【问题标题】:Agenda.JS: How to schedule an event starting at X time and repeat there after every month?Agenda.JS:如何安排从 X 时间开始的活动并在每个月之后重复?
【发布时间】:2018-04-07 23:11:36
【问题描述】:

我想安排这样的任务:

  1. 从 11 月 1 日开始安排任务
  2. 之后每月在那里重复该任务
  3. 我不想在计划仅从 11 月 1 日开始的任务时立即运行它。

我正在使用Agenda.js,我需要确保我正确执行此操作,尤其是第 3 点。它无法在计划的那一刻运行。

这就是我的想法:

const Agenda = require('agenda');
const agenda = new Agenda({db: { address:'mongodb://127.0.0.1/agenda' } });

agenda.define('task', (job, done) => {
    console.log('The task is running', job.attrs.hello);
    done();
});


agenda.run(() => {
   var event = agenda.create('task', { hello: 'world' })
   event.schedule(new Date('2017-11-01'));
   event.repeatEvery('1 month'); // Will this run every month from now or after 2017-11-01?
   agenda.start();
})

但是,我不确定这条线的表现如何:

event.repeatEvery('1 month'); 

问题:从现在开始每月运行一次还是在 2017-11-01 之后运行?

【问题讨论】:

    标签: javascript node.js scheduled-tasks scheduler agenda


    【解决方案1】:

    这也是他们 github 上的一个常见问题。从我在这里找到的[议程问题 758][1]。您所要做的就是在调度调用结束时附加调用 repeatEvery。

    所以你的例子来自:

    agenda.run(() => {
       var event = agenda.create('task', { hello: 'world' })
       event.schedule(new Date('2017-11-01'));
       event.repeatEvery('1 month'); // Will this run every month from now or after 2017-11-01?
       agenda.start();
    })

    到:

    agenda.run(() => {
       var event = agenda.create('task', { hello: 'world' })
       event.schedule(new Date('2017-11-01')).repeatEvery('1 month'); 
       agenda.start();
    })

    回复晚了,但我回答了,因为我发现这个答案很难找到。 [1]:https://github.com/agenda/agenda/issues/758

    【讨论】:

      【解决方案2】:

      由于贾斯汀的解决方案完美运行,我认为您也可以使用 cron 格式并指定重复间隔,如:

      agenda.run(() => {
        var event = agenda.create('task', { hello: 'world' })
        event.schedule(new Date('2017-11-01')).repeatEvery('0 0 1 * *'); 
        agenda.start();
      })
      

      【讨论】:

        【解决方案3】:

        我正在构建一个“30 天”行不通的基于服务的应用程序。可能每 4 周一次。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-19
          • 2015-05-02
          • 2020-06-03
          • 2018-07-31
          相关资源
          最近更新 更多