Valip
Valip

Reputation: 4650

nodeJS agenda start job on a specific date

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.

Upvotes: 4

Views: 2067

Answers (3)

Irfan Shaikh
Irfan Shaikh

Reputation: 1

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

Upvotes: 0

Sunday  Aroh
Sunday Aroh

Reputation: 31

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

Upvotes: 0

Sam Quinn
Sam Quinn

Reputation: 3881

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: '[email protected]',
    subject: 'Email Report',
    body: '...'
  }, done);
});

(async function() {
  await agenda.start();
  await agenda.schedule('tomorrow at 5pm', 'send email report', {to: '[email protected]'});
})();

Upvotes: 0

Related Questions