3

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?

Adam Halasz
  • 57,421
  • 66
  • 149
  • 213

3 Answers3

3

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

Justin
  • 31
  • 2
0

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();
})
F.MANNOU
  • 23
  • 1
  • 7
-1

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

jboyer777
  • 33
  • 8