I would like to schedule tasks like this:
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?
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
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