Class: Tlopo::Retry

Inherits:
Object
  • Object
show all
Extended by:
Setters
Defined in:
lib/tlopo/retry.rb,
lib/tlopo/retry/version.rb

Constant Summary collapse

VERSION =
'0.2.1'

Instance Method Summary collapse

Methods included from Setters

make_setter

Constructor Details

#initialize(**args) ⇒ Retry

Returns a new instance of Retry.



25
26
27
28
29
30
# File 'lib/tlopo/retry.rb', line 25

def initialize(**args)
  @tries = args[:tries] || 3
  @interval = args[:interval] || 1
  @exponential_backoff = args[:exponential_backoff] || false
  @error_types = args[:error_types] || [StandardError]
end

Instance Method Details

#run(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tlopo/retry.rb', line 32

def run(&block)
  count = 1
  while count <= @tries
    next unless block_given?

    begin
      return instance_eval(&block)
    rescue *@error_types => e
      time = @exponential_backoff ? @interval**count : @interval
      sleep time
      count += 1
      e
    end
  end
  LOGGER.error e
  raise "Retries exhausted, error: #{e.message}"
end