program_bumble_bee
program_bumble_bee

Reputation: 305

Using Agenda for job schedule not running

I am using Agenda to schedule my job at a certain time. The job will contain a certain array of emails that will be sent automated emails in the interval of 10 seconds, but the issue is the job is not executing as expected.

The scheduler runs immediately regardless of the scheduled time set as 'after 2 minutes'. On starting the server, the job runs immediately and continue to do so. Along with that, it prints out all the available entries in the database.

This is how I am scheduling the job:

let Agenda = require("agenda");

let agenda = new Agenda();
agenda.database(`localhost:27017/${process.env.DB_NAME}`, "schedulers", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

var i = 0;
agenda.define("campaignScheduler", function (job) {
  i++;
  console.log(
    i +
      " Run at " +
      new Date().getHours() +
      ":" +
      new Date().getMinutes() +
      ":" +
      new Date().getSeconds()
  );
});
const testData = {
  name: "John Snow",
  userId: "j0dn.j213b455b.bchds0",
};

agenda.start();
agenda.on("ready", () => {
  agenda
    .create("campaignScheduler", testData)
    .unique({ "data.unique_id": testData.userId })
    .repeatEvery("*/10 * * * * *", {
      timezone: "America/Los_Angeles",
    })
    .schedule("in 2 minutes")
    .save();

  agenda.on("complete", (job) => {
    console.log(`finished job`, job.attrs);
  });

  agenda.on("job success", (job) => {
    console.log(`Successfull`);
  });

  agenda.on("job fail", (err, job) => {
    console.log(`Job failed with error: ${err.message}`);
  });
});

let graceful = () => {
  agenda.stop(() => process.exit(0));
};

process.on("SIGTERM", graceful);
process.on("SIGINT", graceful);

Results on the console:

enter image description here

Results on db:

enter image description here

The results saving to the database are quite confusing. Please help around to resolve this issue.

Upvotes: 0

Views: 1981

Answers (1)

Kuba Serafinowski
Kuba Serafinowski

Reputation: 61

The scheduler runs immediately regardless of the scheduled time set as 'after 2 minutes'.

It looks like agenda is doing what it's supposed to, as per docs:

repeatEvery(interval, [options])

Specifies an interval on which the job should repeat. The job runs at the time of defining as well in configured intervals, that is "run now and in intervals".

Additionally you're trying to schedule a run every X seconds which isn't possible with a cron expression - as explained here. If you want to spread the emails in time may I suggest queueing them elsewhere in your codebase, the same way you would rate-limit HTTP requests.

Along with that, it prints out all the available entries in the database.

You are printing job.attrs, and that's what it looks like, since what you get with the event is the whole job instance. If you want to print just the supplied data then change your code to:

agenda.on("complete", (job) => {
  console.log(`finished job`, job.attrs.data); // <- add .data
});

Upvotes: 1

Related Questions