module Take2::ClassMethods

Public Instance Methods

backoff_strategy(options) click to toggle source

Sets the backoff strategy

Example:

class PizzaService
  include Take2
  backoff_strategy type: :exponential, start: 3
end

Arguments:

hash: object
# File lib/take2.rb, line 158
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 = {}) { || ... } click to toggle source
# File lib/take2.rb, line 70
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
Also aliased as: with_retry
number_of_retries(num) click to toggle source

Sets number of retries.

Example:

class PizzaService
  include Take2
  number_of_retries 3
end

Arguments:

num: integer
# File lib/take2.rb, line 100
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) click to toggle source

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.
# File lib/take2.rb, line 144
def on_retry(proc)
  raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call)
  self.retry_proc = proc
end
retriable_condition(proc) click to toggle source

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
# File lib/take2.rb, line 130
def retriable_condition(proc)
  raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call)
  self.retry_condition_proc = proc
end
retriable_configuration() click to toggle source

Exposes current class configuration

# File lib/take2.rb, line 165
def retriable_configuration
  Take2::Configuration::CONFIG_ATTRS.each_with_object({}) do |key, hash|
    hash[key] = send(key)
  end
end
retriable_errors(*errors) click to toggle source

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 retriable errors
# File lib/take2.rb, line 114
def retriable_errors(*errors)
  message = 'All retriable errors must be StandardError descendants'
  raise ArgumentError, message unless errors.all? { |e| e <= StandardError }
  self.retriable = errors
end
with_retry(options = {})
Alias for: call_api_with_retry

Private Instance Methods

next_interval(intervals, retries, current) click to toggle source
# File lib/take2.rb, line 191
def next_interval(intervals, retries, current)
  intervals[retries - current]
end
response_status(response) click to toggle source
# File lib/take2.rb, line 181
def response_status(response)
  return response.status if response.respond_to?(:status)
  response.status_code if response.respond_to?(:status_code)
end
rest(config, tries) click to toggle source
# File lib/take2.rb, line 186
def rest(config, tries)
  seconds = next_interval(config[:backoff_intervals], config[:retries], tries)
  sleep(seconds)
end
set_defaults() click to toggle source
# File lib/take2.rb, line 175
def set_defaults
  Take2.config.to_hash.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
end