Module: Take2::ClassMethods
- Defined in:
- lib/take2.rb
Instance Method Summary collapse
-
#backoff_strategy(options) ⇒ Object
Sets the backoff strategy.
-
#number_of_retries(num) ⇒ Object
Sets number of retries.
-
#on_retry(proc) ⇒ Object
Defines a proc that is called before retry attempt.
-
#retriable_condition(proc) ⇒ Object
Sets condition for retry attempt.
-
#retriable_configuration ⇒ Object
Exposes current class configuration.
-
#retriable_errors(*errors) ⇒ Object
Sets list of errors on which the block will retry.
- #sleep_before_retry(seconds) ⇒ Object
Instance Method Details
#backoff_strategy(options) ⇒ Object
Sets the backoff strategy
Example:
class PizzaService
include Take2
backoff_strategy type: :exponential, start: 3
end
Arguments:
hash: object
172 173 174 175 176 |
# File 'lib/take2.rb', line 172 def backoff_strategy() available_types = [:constant, :linear, :fibonacci, :exponential] raise ArgumentError, 'Incorrect backoff type' unless available_types.include?([:type]) self.backoff_intervals = Backoff.new([:type], [:start]).intervals end |
#number_of_retries(num) ⇒ Object
Sets number of retries.
Example:
class PizzaService
include Take2
number_of_retries 3
end
Arguments:
num: integer
105 106 107 108 |
# File 'lib/take2.rb', line 105 def number_of_retries(num) raise ArgumentError, 'Must be positive Integer' unless num.is_a?(Integer) && num.positive? self.retries = num end |
#on_retry(proc) ⇒ Object
Defines a proc that is called before retry attempt.
Example:
class PizzaService
include Take2
on_retry proc { |error, tries| puts "Retrying.. #{tries} of #{self.class.retriable_configuration[:retries]}" }
end
Arguments:
proc: Proc. The proc called by default with the raised error and number of left retries.
149 150 151 152 |
# File 'lib/take2.rb', line 149 def on_retry(proc) raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call) self.retry_proc = proc end |
#retriable_condition(proc) ⇒ Object
Sets condition for retry attempt. If set, it MUST result to false
with number left retries greater that zero in order to retry.
Example:
class PizzaService
include Take2
retriable_condition proc { |error| error.response.status_code < 500 }
end
Arguments:
proc: Proc. The proc called by default with the raised error argument
135 136 137 138 |
# File 'lib/take2.rb', line 135 def retriable_condition(proc) raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call) self.retry_condition_proc = proc end |
#retriable_configuration ⇒ Object
Exposes current class configuration
179 180 181 182 183 |
# File 'lib/take2.rb', line 179 def retriable_configuration Take2::Configuration::CONFIG_ATTRS.each_with_object({}) do |key, hash| hash[key] = send(key) end end |
#retriable_errors(*errors) ⇒ Object
Sets list of errors on which the block will retry.
Example:
class PizzaService
include Take2
retriable_errors Net::HTTPRetriableError, Errno::ECONNRESET
end
Arguments:
errors: List of retiable errors
119 120 121 122 123 |
# File 'lib/take2.rb', line 119 def retriable_errors(*errors) = 'All retriable errors must be StandardError decendants' raise ArgumentError, unless errors.all? { |e| e <= StandardError } self.retriable = errors end |
#sleep_before_retry(seconds) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/take2.rb', line 154 def sleep_before_retry(seconds) unless (seconds.is_a?(Integer) || seconds.is_a?(Float)) && seconds.positive? raise ArgumentError, 'Must be positive numer' end puts "DEPRECATION MESSAGE - The sleep_before_retry method is softly deprecated in favor of backoff_stategy \r where the time to sleep is a starting point on the backoff intervals. Please implement it instead." self.time_to_sleep = seconds end |