0

I am trying to re-create all the expired job at the server start.

(async function() {
  await agenda.start();
  const jobs = await agenda.jobs({nextRunAt: {$lt: new Date(Date.now())}});
  for(var job of jobs){
    var { _id, name, data, type, priority, nextRunAt, lastModifiedBy, lockedAt, lastRunAt} = job.attrs;
    var newDate = Date.now() + 30000;
    await agenda.cancel({_id:_id});
    await agenda.schedule(newDate,name,data);
  }
})();

However, while the new jobs are saved in the database, they are not executed.

Any idea ?

John doe
  • 3,680
  • 7
  • 31
  • 65
  • why are you using agenda.cancel? when the server is restarted every scheduled should be cancelled. – Ahmed Magdy Dec 20 '20 at 13:29
  • What if my server shut down unexpectedly ? Also, I've also tried without `cancel` and the result is the same. When my server restart every scheduled job are not cancelled. Those that are not expired are well executed. Only the outdated one are lost. How to handle server downtime when using `Agenda` ? – John doe Dec 20 '20 at 16:49
  • i've actually wrote an article about how to build a cronjobs server check it out from [here](https://dev.to/ahmedmagdy11/building-a-cronjob-server-with-nodejs-1dgi) – Ahmed Magdy Dec 20 '20 at 16:55
  • also, try to execute jobs immediately to see if it's a time problem or no. – Ahmed Magdy Dec 20 '20 at 16:56

0 Answers0