Class: Wrappi::Executer::Retryer

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappi/executer/retryer.rb

Defined Under Namespace

Classes: RetryError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Retryer

Returns a new instance of Retryer.



6
7
8
# File 'lib/wrappi/executer/retryer.rb', line 6

def initialize(endpoint)
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



5
6
7
# File 'lib/wrappi/executer/retryer.rb', line 5

def endpoint
  @endpoint
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/wrappi/executer/retryer.rb', line 10

def call
  if retry?
    Retryable.retryable(retry_options) do
      res = yield
      raise RetryError if retry_if.call(res, endpoint)
      res
    end
  else
    yield
  end
end

#retry?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/wrappi/executer/retryer.rb', line 37

def retry?
  !!endpoint.retry_if
end

#retry_ifObject



41
42
43
# File 'lib/wrappi/executer/retryer.rb', line 41

def retry_if
  endpoint.retry_if
end

#retry_optionsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wrappi/executer/retryer.rb', line 22

def retry_options
  default = { tries: 3, on: [RetryError] }
  if endpoint.retry_options
    end_opts = endpoint.retry_options.dup
    {}.tap do |h|
      h[:tries] = end_opts[:tries] || default[:tries]
      if on = end_opts.delete(:on)
        h[:on] = default[:on] + Fusu::Array.wrap(on)
      end
    end.merge(end_opts)
  else
    default
  end
end