Blog / Max attempts for a single delayed_job Job

May 15, 2013

Delayed_job, the asynchronous priority queue system has a well documented configuration for setting the max number of attempts a worker will run for jobs in the queue:

# config/initializers/delayed_job_config.rb
  Delayed::Job.destroy_failed_jobs = false
  silence_warnings do
    Delayed::Job.const_set("MAX_ATTEMPTS", 3)
    Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes)

There is also a way to get more granular control over attempts at a job level instead of the worker level. This is defined in the base class of the gem.

Delayed_job first checks to see if the payload_object of a job has a max_attempts value and if it doesn’t it falls back to the max_attempts value defined in the worker.

So all you have to do is to define an instance method in your payload_object’s class for the max number of attempts for a job:

def max_attempts; 1; end