Search code examples
ruby-on-railsdelayed-job

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?


Solution

  • 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.