Search code examples
ruby-on-railsrubydelayed-job

Avoiding duplicate jobs in delayed_job queue


I'm having an issue avoiding duplicate jobs being added to my applications the delayed job queue. I've done a bit of searching, but all have been fruitless. Any suggestions on how to implement a solution would be appreciated.

Links I've stumbled upon, but offer no solutions.

  1. https://github.com/collectiveidea/delayed_job/issues/72
  2. https://groups.google.com/forum/#!topic/delayed_job/gZ9bFCdZrsk

Solution

  • I would not focus on avoiding duplicate jobs. In my opinion, a job queue should be dump and not hold a state. Adding a job to the queue should be as fast as possible. Any check upfront would slow down the response time.

    Instead of checking for duplicates upfront, the job itself should be clever enough to figure out if it is still appropriate to run. The job could for example first check a status, a flag, or a timestamp on a model. And only if that precondition is still valid start with the actual processing.