How do I run cron-like job at certain date time in the future using Agenda in Nodejs

5.5k views Asked by At

I have a problem here. How do i run job at certain date time in the future using Agenda like node-scheduler do. Based on https://www.npmjs.com/package/node-schedule, node-schedule have cron-style scheduling method. So it is easier for me to extract date from my input. I have read Agenda documentation https://github.com/rschmukler/agenda#agenda-events, it says Agenda uses Human Interval for specifying the intervals.

Cron

How can i do this?

2

There are 2 answers

7
Aruna On BEST ANSWER

If you have a future date, you can convert this to number of days from today or even number of seconds as below and then use the same with agenda cron job

function findDaysDifference ( date1, date2 ) {
  //Get 1 day in milliseconds
  var oneDay_ms = 1000 * 60 * 60 * 24;

  // Convert both dates to milliseconds
  var date1_ms = date1.getTime();
  var date2_ms = date2.getTime();

  // Calculate the difference in milliseconds
  var difference_ms = date2_ms - date1_ms;
    
  // Convert back to days and return
  return Math.round(difference_ms/oneDay_ms); 
}

var futureDate = new Date(2018, 0, 1);
var daysFromNow = findDaysDifference(new Date(), futureDate);

console.log(daysFromNow);

Then create agenda job and schedule it with daysFromNow calculated above,

agenda.define('sayHello', function(job) {
  console.log("Hello!");
});

// Schedule a job to run once at a given time
agenda.schedule(daysFromNow + ' days', 'sayHello');

If you want to schedule it at particular time in the future, you can calculate the seconds as below,

function findSecondsDifference ( date1, date2 ) { 
      var oneSecond_ms = 1000;
  
      // Convert both dates to milliseconds
      var date1_ms = date1.getTime();
      var date2_ms = date2.getTime();

      // Calculate the difference in milliseconds
      var difference_ms = date2_ms - date1_ms;
        
      // Convert back to days and return
      return Math.round(difference_ms/oneSecond_ms); 
    }

    var futureDate = new Date(2018, 0, 1, 16);
    var secsFromNow = findSecondsDifference(new Date(), futureDate);

    console.log(secsFromNow);

Then create agenda job and schedule it with secsFromNow calculated above,

agenda.define('sayHello', function(job) {
  console.log("Hello!");
});

// Schedule a job to run once at a given time
agenda.schedule(secsFromNow + ' seconds', 'sayHello');
0
Eshemiedomi Samuel Oghenevwede On

With Agenda.js

interval can be a human-readable format String, a cron format String, or a Number.

meaning it supports cron-style scheduling method.

for example

you could schedule a job to run 3 minutes from the current time

  await agenda.schedule('3 minutes', 'send confirmation to new user', {email:"[email protected]"} );

  // Alternatively, you could also do:
  await agenda.schedule('*/3 * * * *', 'send confirmation to new user', {email:"[email protected]"});
   

run a job Monday at 09:00

   await agenda.schedule('0 9 * * 1', 'MondayJob', {msg:"guess what today is 'Monday'"});

run on Saturday at 23:45 (11:45 PM)

await agenda.schedule('45 23 * * 6', 'jobName', {});