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, #identifier, #on_failure_retry, #redis_retry_key, #retry_attempt, #retry_criteria_valid?, #retry_exception?, #retry_exceptions, #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



62
63
64
# File 'lib/resque/plugins/exponential_backoff.rb', line 62

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.



53
54
55
# File 'lib/resque/plugins/exponential_backoff.rb', line 53

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



45
46
47
# File 'lib/resque/plugins/exponential_backoff.rb', line 45

def retry_limit
  @retry_limit ||= backoff_strategy.length
end