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

Created on 26 Oct 2017  路  3Comments  路  Source: agenda/agenda

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?

Most helpful comment

Hey

I was struggling the same issues, I did something like this and it works:

var job = agenda.create("jobName",data); job.schedule(...); job.repeatEvery(...); job.save();

Best Regards
JB

All 3 comments

Hey

I was struggling the same issues, I did something like this and it works:

var job = agenda.create("jobName",data); job.schedule(...); job.repeatEvery(...); job.save();

Best Regards
JB

@adamhalasz I had a similar issue. Couldn't find a good solution. However this workaround fits for me:

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

agenda.define('task', (job, done) => {
   if (job.attrs.startNow) console.log('The task is running', job.attrs.hello);
   else console.log('Sorry, I am not ready now.');
   job.attrs.startNow = true;
   done();
});


agenda.run(() => {
   var event = agenda.create('task', { hello: 'world', startNow: false })
   event.repeatEvery('2 minutes');
   agenda.start();
})

UPDATE:
I realized that you actually don't have to set startNow in the create function. But that's cleaner :-)

This might get easier in the future: https://github.com/agenda/agenda/pull/580

Was this page helpful?
0 / 5 - 0 ratings