Module: Take2::ClassMethods

Defined in:
lib/take2.rb

Instance Method Summary collapse

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

Raises:

  • (ArgumentError)


162
163
164
165
166
# File 'lib/take2.rb', line 162

def backoff_strategy(options)
  available_types = [:constant, :linear, :fibonacci, :exponential]
  raise ArgumentError, 'Incorrect backoff type' unless available_types.include?(options[:type])
  self.backoff_intervals = Backoff.new(options[:type], options[:start]).intervals
end

#call_api_with_retry(options = {}) ⇒ Object



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(options = {})
  config = retriable_configuration
  config.merge!(Take2.local_defaults(options)) unless options.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

Raises:

  • (ArgumentError)


95
96
97
98
# File 'lib/take2.rb', line 95

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.

Raises:

  • (ArgumentError)


139
140
141
142
# File 'lib/take2.rb', line 139

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

Raises:

  • (ArgumentError)


125
126
127
128
# File 'lib/take2.rb', line 125

def retriable_condition(proc)
  raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call)
  self.retry_condition_proc = proc
end

#retriable_configurationObject

Exposes current class configuration



169
170
171
172
173
# File 'lib/take2.rb', line 169

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

Raises:

  • (ArgumentError)


109
110
111
112
113
# File 'lib/take2.rb', line 109

def retriable_errors(*errors)
  message = 'All retriable errors must be StandardError decendants'
  raise ArgumentError, message unless errors.all? { |e| e <= StandardError }
  self.retriable = errors
end

#sleep_before_retry(seconds) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/take2.rb', line 144

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