简体   繁体   中英

Agenda.JS: How to schedule an event starting at X time and repeat there after every month?

I would like to schedule tasks like this:

  1. schedule a task starting on November 1st
  2. Repeat the task every month there after
  3. I don't want to run it right at the moment when the task is scheduled only beginning November 1st.

I'm using Agenda.js and I need to make sure that I do this correctly especially Point 3. It cannot run at the moment when it is scheduled.

This is what I have in mind:

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

However, I'm not sure how would this line behave:

event.repeatEvery('1 month'); 

Question: Will this run every month from now or after 2017-11-01?

This is a common question asked on their github too. From what I found here [Agenda issue 758][1]. All you have to do is append the call repeatEvery at the end of the schedule call.

So your example would go from:

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

to:

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

Late response but I answered because I found this answer difficult to find. [1]: https://github.com/agenda/agenda/issues/758

As Justin's solution works perfectly, I think you can also go for cron format and specify the repeat interval like :

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

I'm building a service based app where '30 days' won't work. Maybe every 4 weeks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM