4

Is there any way to start a job on a specific date using the agenda library?

This is how I'm creating the jobs:

const someData = {
  name: 'Name',
  value: 'Value'
}
const job = await agenda.create('daily alert', someData);
await job.repeatEvery('1 day').save() // specify when to start

But at this moment the job starts right after it was created, and I want to somehow specify the date when it should start, like tomorrow.

Valip
  • 4,440
  • 19
  • 79
  • 150

3 Answers3

0

Use agenda.schedule(when, job, data)

Schedules a job to run name once at a given time. when can be a Date or a String such as tomorrow at 5pm. https://github.com/agenda/agenda#schedulewhen-name-data-cb


agenda.define('send email report', {priority: 'high', concurrency: 10}, (job, done) => {
  const {to} = job.attrs.data;
  emailClient.send({
    to,
    from: 'example@example.com',
    subject: 'Email Report',
    body: '...'
  }, done);
});

(async function() {
  await agenda.start();
  await agenda.schedule('tomorrow at 5pm', 'send email report', {to: 'admin@example.com'});
})();
Sam Quinn
  • 3,310
  • 1
  • 16
  • 11
  • This only runs the job once, I still want it to repeat every x days. – Valip Feb 25 '19 at 07:51
  • Anyluck ? I also want to implement this every day at 12 pm – Robokishan Apr 30 '20 at 14:01
  • 1
    You can use `agenda.every('day at 12pm')` Agenda internally uses the package `human-interval` which parses the schedule format. More information in the documentation -> https://github.com/agenda/agenda#creating-jobs – Sam Quinn May 03 '20 at 17:12
0

This will work for you.

const scheduleAJobOnlyOnce = (options) => {
    let job = this.Agenda.create(options.jobName, options);
    job.schedule(options.start); // run at this time
    job.save(); 
  };
Sunday Aroh
  • 21
  • 1
  • 4
0

Define a "job", an arbitrary function that agenda can execute

const dt = new Date();

dt.setMinutes(dt.getMinutes()+60); // run after an hour

agenda.schedule(dt, 'hello'); // this will do it

agenda.start();
Tyler2P
  • 2,324
  • 26
  • 22
  • 31