6

We have been using agenda for sometime in our node.js server and are confused about how the job locking mechanism works in agenda.

  • Sometimes we see that the 'lockedAt' field for a job in the database has a non-null value and it changes to null suddenly.
  • Sometimes, the 'lockedAt' value stays as null and jobs get stuck.
  • Sometimes, after 10 minutes the 'lockedAt' value gets updated, but still the job is stuck.

And if I restart the node.js server, all the locked jobs get unlocked and get executed properly.

Why is this locking mechanism needed ? What does it do ?

Sampath Kumar
  • 165
  • 2
  • 10

1 Answers1

5

When you running a Job, the db entry will automatically set a lockedAt timestamp, this ensure no other node instance will run the same job. It means "hey I am working on this job, don't touch it"

When another node instance read the job, it checks current time against the lock timestamp, if it has been less than 10 minutes, it will not run the job.

This "10 minutes" is stored in agenda global config or job config options, both are from code. You can change it by setting defaultlocklifetimenumber to a different value.

Mark Ni
  • 2,383
  • 1
  • 27
  • 33