Class: Amazon::RetryDelegator

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/retry_delegator.rb

Overview

RetryDelegator

this is a wrapper around a client that will retry if exceptions are raised.

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ RetryDelegator

Returns a new instance of RetryDelegator.



11
12
13
14
15
16
17
18
19
20
# File 'lib/amazon/retry_delegator.rb', line 11

def initialize(client, options={})
  @client            = client
  @log               = options[:log] || StdErrLogger.new
  @backoff_seconds   = options[:backoff_seconds] || 2
  @backoff_mult      = options[:backoff_mult] || 1.5
  @retries           = options[:retries] || 8
  @retry_if          = options[:retry_if]
  @pass_exceptions   = options[:pass_exceptions] || [ScriptError, SignalException, ArgumentError, StandardError]
  @retry_exceptions  = options[:retry_exceptions] || [IOError, EOFError, RuntimeError]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/amazon/retry_delegator.rb', line 38

def method_missing(method, *args)
  backoff_seconds = @backoff_seconds
  backoff_mult = @backoff_mult
  retries_remaining = @retries
  begin
    response = @client.send(method, *args)
    if @retry_if && @retry_if.call(response) then
      raise "Retriable invalid response returned from #{method}: #{response.inspect}"
    end
    return response
  rescue Exception => e
    if retries_remaining > 0 && is_retry_exception(e) then
      if @log != nil then
        @log.info "Exception #{e} while calling #{method} on #{@client.class}, retrying in #{@backoff_seconds * backoff_mult} seconds."
      end
      sleep(@backoff_seconds * backoff_mult)
      backoff_mult *= 2
      retries_remaining -= 1
      retry
    else
      if @log != nil then
        @log.info "Exception #{e} while calling #{method} on #{@client.class}, failing"
      end
      raise e
    end
  end
end

Instance Method Details

#is_retry_exception(e) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/amazon/retry_delegator.rb', line 22

def is_retry_exception(e)
  if @retry_exceptions then
    for retry_exception in @retry_exceptions do
      return true if e.is_a?(retry_exception)
    end
  end
  if @pass_exceptions then
    for pass_exception in @pass_exceptions do
      return false if e.is_a?(pass_exception)
    end
    return true
  else
    return false
  end
end