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.retry_identifier(mt_id, mobile_number, message)
"#{mobile_number}:#{mt_id}"
end
self.perform(mt_id, mobile_number, message)
heavy_lifting
end
end
Defined Under Namespace
Classes: InvalidRetryDelayMultiplicandConfigurationException
Constant Summary collapse
- DEFAULT_RETRY_DELAY_MULTIPLICAND_MIN =
Constants
1.0
- DEFAULT_RETRY_DELAY_MULTIPLICAND_MAX =
1.0
Instance Attribute Summary
Attributes included from Retry
Class Method Summary collapse
-
.extended(receiver) ⇒ Object
private
Fail fast, when extended, if the “receiver” is misconfigured.
Instance Method Summary collapse
-
#backoff_strategy ⇒ Array
abstract
The backoff strategy is used to vary the delay between retry attempts.
-
#retry_delay ⇒ Number
private
Selects the delay from the backoff strategy.
-
#retry_delay_multiplicand_max ⇒ Float
abstract
The maximum value (upper-bound) for the range that is is used in calculating the retry-delay product.
-
#retry_delay_multiplicand_min ⇒ Float
abstract
The minimum value (lower-bound) for the range that is is used in calculating the retry-delay product.
-
#retry_limit ⇒ Number
private
Defaults to the number of delays in the backoff strategy.
Methods included from Retry
#after_perform_retry, #args_for_retry, #before_perform_retry, #clean_retry_key, #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_identifier, #retry_job_delegate, #retry_limit_reached?, #sleep_after_requeue, #try_again
Class Method Details
.extended(receiver) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fail fast, when extended, if the “receiver” is misconfigured
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 54 def self.extended(receiver) retry_delay_multiplicand_min = \ receiver.instance_variable_get("@retry_delay_multiplicand_min") || \ DEFAULT_RETRY_DELAY_MULTIPLICAND_MIN retry_delay_multiplicand_max = \ receiver.instance_variable_get("@retry_delay_multiplicand_max") || \ DEFAULT_RETRY_DELAY_MULTIPLICAND_MAX if retry_delay_multiplicand_min > retry_delay_multiplicand_max raise InvalidRetryDelayMultiplicandConfigurationException.new( %{"@retry_delay_multiplicand_min" must be less than or equal to "@retry_delay_multiplicand_max"} ) end end |
Instance Method Details
#backoff_strategy ⇒ Array
The backoff strategy is used to vary the delay between retry attempts
117 118 119 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 117 def backoff_strategy @backoff_strategy ||= [0, 60, 600, 3600, 10_800, 21_600] end |
#retry_delay ⇒ Number
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Selects the delay from the backoff strategy
82 83 84 85 86 87 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 82 def retry_delay delay = backoff_strategy[retry_attempt] || backoff_strategy.last delay_multiplicand = \ rand(retry_delay_multiplicand_min..retry_delay_multiplicand_max) (delay * delay_multiplicand).to_i end |
#retry_delay_multiplicand_max ⇒ Float
The maximum value (upper-bound) for the range that is is used in calculating the retry-delay product
107 108 109 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 107 def retry_delay_multiplicand_max @retry_delay_multiplicand_max ||= DEFAULT_RETRY_DELAY_MULTIPLICAND_MAX end |
#retry_delay_multiplicand_min ⇒ Float
The minimum value (lower-bound) for the range that is is used in calculating the retry-delay product
96 97 98 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 96 def retry_delay_multiplicand_min @retry_delay_multiplicand_min ||= DEFAULT_RETRY_DELAY_MULTIPLICAND_MIN end |
#retry_limit ⇒ Number
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defaults to the number of delays in the backoff strategy
73 74 75 |
# File 'lib/resque/plugins/exponential_backoff.rb', line 73 def retry_limit @retry_limit ||= backoff_strategy.length end |