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:
Results on db:
The results saving to the database are quite confusing. Please help around to resolve this issue.