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, )
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
-
#backoff_strategy ⇒ Array
abstract
The backoff strategy is used to vary the delay between retry attempts.
-
#retry_delay ⇒ Number
Selects the delay from the backoff strategy.
-
#retry_limit ⇒ Number
Defaults to the number of delays in the backoff strategy.
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_strategy ⇒ Array
This method is abstract.
The backoff strategy is used to vary the delay between retry attempts.
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_delay ⇒ Number
Selects the delay from the backoff strategy.
53 54 55 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 53 def retry_delay backoff_strategy[retry_attempt] || backoff_strategy.last end |
#retry_limit ⇒ Number
Defaults to the number of delays in the backoff strategy.
45 46 47 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 45 def retry_limit @retry_limit ||= backoff_strategy.length end |