1

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?

Sarmad
  • 303
  • 3
  • 16

1 Answers1

2

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.

Akrion
  • 18,117
  • 1
  • 34
  • 54
  • Thanks @Akrion. I have had a look at that too during my research but this doesn't work for me because lockedAt is already null as the job has not started. For my case, as a workaround I fetch all the jobs where nextRunAt > current datetime and reschedule them. Which create duplicate jobs so I delete the duplicates afterwards. – Sarmad Sep 30 '18 at 14:16
  • But wait the above code would do that for you. It looks if there is a field `lockedAt` and does not care about its value. It also does not delete the records but just resets the fields. It is a better approach I would argue. – Akrion Sep 30 '18 at 23:15
  • But if you do not reschedule them it would not work anyway – Hairi Nov 02 '21 at 19:24