0

I have a Node-Express application, in which I have got the logic to create Agenda jobs dynamically.

I am following the documentation of Agenda, and have got similar project structure.

The main difference is that, in the controller action, I am defining a new job, instead of calling agenda.now()

This is my controller action

const createNewJob = (req, res, next) => {
  const {username, type} = req.body;

  const jobName = username + type;

  agenda.define(jobName, async job => {
    console.log('Running new job');
  });
  
  agenda.every('2 minutes', jobName);
}

This works fine and runs the job every 2 minutes as long as the server is running. But, if the server has been restarted, the job doesn't trigger even if the nextRunAt is in future.

I can see that the job is NOT locked in DB. I tried the solution mentioned in this repo too. That didn't work either.

Also, tried adding agenda.start() during server startup (after the call to connect to MongoDB). No luck with that too.

agenda.on("ready", function() {
  agenda.start();
  console.log('Starting agenda...');
});

Would be great if anyone can suggest what I am missing here.

Thanks in advance.

CodeBird
  • 387
  • 1
  • 8

0 Answers0