Search code examples
ruby-on-railsrubyruby-on-rails-3resque

Resque retry to perform a 'finally' function after several retries


I have a resque job that is supposed to call a third-party API. I want this perform method to retry at least 3 times. If it still does not go through on the third try, I want it to send an e-mail to me saying that something went wrong and the API could not be called.

Is there a way to do this using resque-retry


Solution

  • You could use custom retry criteria to check how many times resque-retry has retried for you and do something different if the number is too large. Something like this:

    class APIWorker
      extend Resque::Plugins::Retry
      @queue = :api_worker_queue
    
      retry_criteria_check do |exception, *args|
        if retry_attempt > 3
          send_email
    
          false # don't retry anymore
        else
          true # continue retrying
        end
      end
    
      def self.perform(job_id)
        do_api_stuff
      end
    end