Module: Take2::ClassMethods

Defined in:
lib/take2.rb

Instance Method Summary collapse

Instance Method Details

#number_of_retries(num) ⇒ Object

Sets number of retries.

Example:

class PizzaService
  include Take2
  number_of_retries 3
end

Arguments:

num: integer

Raises:

  • (ArgumentError)


89
90
91
92
# File 'lib/take2.rb', line 89

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)


132
133
134
135
# File 'lib/take2.rb', line 132

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)


118
119
120
121
# File 'lib/take2.rb', line 118

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



152
153
154
155
156
# File 'lib/take2.rb', line 152

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)


103
104
105
106
# File 'lib/take2.rb', line 103

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

#sleep_before_retry(seconds) ⇒ Object

Sets number of seconds to sleep before next retry.

Example:

class PizzaService
  include Take2
  sleep_before_retry 1.5
end

Arguments:

seconds: number

Raises:

  • (ArgumentError)


146
147
148
149
# File 'lib/take2.rb', line 146

def sleep_before_retry(seconds)
  raise ArgumentError, 'Must be positive numer' unless (seconds.is_a?(Integer) || seconds.is_a?(Float)) && seconds.positive?
  self.time_to_sleep = seconds
end