Module: Take2::ClassMethods
- Defined in:
- lib/take2.rb
Instance Method Summary collapse
-
#backoff_strategy(options) ⇒ Object
Sets the backoff strategy.
- #call_api_with_retry(options = {}) ⇒ Object (also: #with_retry)
-
#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
164 165 166 167 168 |
# File 'lib/take2.rb', line 164 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 |
#call_api_with_retry(options = {}) ⇒ Object Also known as: with_retry
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/take2.rb', line 67 def call_api_with_retry( = {}) config = retriable_configuration config.merge!(Take2.local_defaults()) unless .empty? tries ||= config[:retries] begin yield rescue => e if config[:retriable].map { |klass| e.class <= klass }.any? unless tries.zero? || config[:retry_condition_proc]&.call(e) config[:retry_proc]&.call(e, tries) rest(config, tries) tries -= 1 retry end end raise e end end |
#number_of_retries(num) ⇒ Object
Sets number of retries.
Example:
class PizzaService
include Take2
number_of_retries 3
end
Arguments:
num: integer
97 98 99 100 |
# File 'lib/take2.rb', line 97 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.
141 142 143 144 |
# File 'lib/take2.rb', line 141 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
127 128 129 130 |
# File 'lib/take2.rb', line 127 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
171 172 173 174 175 |
# File 'lib/take2.rb', line 171 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
111 112 113 114 115 |
# File 'lib/take2.rb', line 111 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
146 147 148 149 150 151 152 153 |
# File 'lib/take2.rb', line 146 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 |