Agenda job doesn't run after server restart

2.5k Views Asked by At

I have an Agenda job that runs perfectly fine when I define it for the first time. I can see the corresponding document in MongoDB and all is well. But when the server restarts or I press 'Ctrl+C' and terminate the process so after re-running my app, the job does not run afterwards.

There has been a lot of discussion about this issue on git as well.

I have tried:

1- Gracefully handling the SIGTERM

2- Run a MongoDB update query and set the lockedAt, lastModifiedBy, lastRunAt to null whenever you start your app.

Neither of the two worked for me. I can see in MongoDB that the job is not in a locked state so I am confused why does it not run if the app is restarted?

1

There are 1 best solutions below

3
On

Well if you have tried everything you might as well just update all jobs that have failed to start with something like this:

  agendaJobs.update({
    lockedAt: {
      $exists: true
    },
    lastFinishedAt: {
      $exists: false
    }
  }, {
    $unset: {
      lockedAt: undefined,
      lastModifiedBy: undefined,
      lastRunAt: undefined
    },
    $set: {
      nextRunAt: new Date()
    }
  }, {
    multi: true
  })

It is one way to brute force the issue and basically set all the locked jobs to be essentially re-scheduled.

This is by the way one of the suggested approaches on git.

It is coming from here if you want to entire context.