Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed Job: Configure run_at and max_attempts for a specific job

I need to overwrite the Delayed::Worker.max_attempts for one specific job, which I want to retry a lot of times. Also, I don't want the next scheduled time to be determined exponentially (From the docs: 5 seconds + N ** 4, where N is the number of retries).

I don't want to overwrite the Delayed::Worker settings, and affect other jobs.

My job is already a custom job (I handle errors in a certain way), so that might be helpful. Any pointers on how to do this?

like image 490
gylaz Avatar asked Apr 26 '13 00:04

gylaz


1 Answers

I figured it out by looking through delayed_job source code. This is not documented anywhere in their docs.

Here's what I did:

class MyCustomJob < Struct.new(:param1, :param2)
  def perform
    # do something
  end
  
  # attempts and time params are required by delayed_job
  def reschedule_at(time, attempts)
    30.seconds.from_now
  end

  def max_attempts
    50
  end
end

Then run it wherever you need to by using enqueue, like this:

Delayed::Job.enqueue( MyCustomJob.new( param1, param2 ) )

Hope this helps someone in the future.

like image 182
gylaz Avatar answered Oct 08 '22 07:10

gylaz