Module: Resque::Plugins::ExponentialBackoff

Includes:
Retry
Defined in:
lib/resque/plugins/exponential_backoff.rb

Overview

If you want your job to retry on failure using a varying delay, simply extend your module/class with this module:

class DeliverSMS
  extend Resque::Plugins::ExponentialBackoff
  @queue = :mt_messages

  def self.perform(mt_id, mobile_number, message)
    heavy_lifting
  end
end

Easily do something custom:

class DeliverSMS
  extend Resque::Plugins::ExponentialBackoff
  @queue = :mt_messages

  @retry_limit = 4

  # retry delay in seconds; [0] => 1st retry, [1] => 2nd..4th retry.
  @backoff_strategy = [0, 60]

  # used to build redis key, for counting job attempts.
  def self.identifier(mt_id, mobile_number, message)
    "#{mobile_number}:#{mt_id}"
  end

  self.perform(mt_id, mobile_number, message)
    heavy_lifting
  end
end

Instance Method Summary collapse

Methods included from Retry

#after_perform_retry, #args_for_retry, #before_perform_retry, #clean_retry_key, #identifier, #inherited, #instance_exec, #on_failure_retry, #redis_retry_key, #retry_attempt, #retry_criteria_check, #retry_criteria_checks, #retry_criteria_valid?, #retry_exception?, #retry_exceptions, #retry_limit_reached?, #try_again

Instance Method Details

#backoff_strategyArray

This method is abstract.

The backoff strategy is used to vary the delay between retry attempts.

Returns:

  • (Array)

    array of delays. index = retry attempt



58
59
60
# File 'lib/resque/plugins/exponential_backoff.rb', line 58

def backoff_strategy
  @backoff_strategy ||= [0, 60, 600, 3600, 10_800, 21_600]
end

#retry_delayNumber

Selects the delay from the backoff strategy.

Returns:

  • (Number)

    seconds to delay until the next retry.



50
51
52
# File 'lib/resque/plugins/exponential_backoff.rb', line 50

def retry_delay
  backoff_strategy[retry_attempt] || backoff_strategy.last
end

#retry_limitNumber

Defaults to the number of delays in the backoff strategy.

Returns:

  • (Number)

    maximum number of retries



43
44
45
# File 'lib/resque/plugins/exponential_backoff.rb', line 43

def retry_limit
  @retry_limit ||= backoff_strategy.length
end