Module: Retryable

Extended by:
Retryable
Included in:
Retryable
Defined in:
lib/retryable.rb

Instance Method Summary collapse

Instance Method Details

#retryable(options = {}) {|handler| ... } ⇒ Object

Yields:

  • (handler)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/retryable.rb', line 4

def retryable(options = {})
  opts    = {:on => Exception, :times => 1}.merge(options)
  handler = {}
  
  retry_exception = opts[:on].is_a?(Array) ? opts[:on] : [opts[:on]]
  times = retries = opts[:times]
  attempts        = 0

  begin
    attempts += 1
    
    return yield(handler)
  rescue *retry_exception => exception
    opts[:then].call(exception, handler, attempts, retries, times) if opts[:then]
    
    if attempts <= times
      sleep(opts[:sleep] || (rand(11) / 100.0)) unless opts[:sleep] == false
      retries -= 1
      retry
    else
      opts[:finally].call(exception, handler, attempts, retries, times) if opts[:finally]
      raise exception
    end
  ensure
    opts[:always].call(handler, attempts, retries, times) if opts[:always]
  end

  yield(handler)
end