Module: ZeevexReliability::Retryable

Defined in:
lib/zeevex_reliability/retryable.rb

Class Method Summary collapse

Class Method Details

.retryable(options = {}, &block) ⇒ Object

Options:

  • :tries - Number of retries to perform. Defaults to 3.

  • :on - The Exception on which a retry will be performed.

    Defaults to StandardError
    

If the final attempts also receives an exception, that exception will be raised.

Example

retryable(:tries => 1, :on => OpenURI::HTTPError) do
  # your code here
end


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zeevex_reliability/retryable.rb', line 17

def self.retryable(options = {}, &block)
  opts = { :tries => 3, :on => StandardError }.merge(options)

  retry_exception, retries = opts[:on], opts[:tries]

  begin
    return yield
  rescue retry_exception
    if (retries -= 1) > 0
      retry
    else
      raise
    end
  end
end