Class: ReeStd::Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/ree_lib/packages/ree_std/package/ree_std/retry.rb

Instance Method Summary collapse

Constructor Details

#initialize(max:, **opts) ⇒ Retry

Returns a new instance of Retry.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ree_lib/packages/ree_std/package/ree_std/retry.rb', line 17

def initialize(max:, **opts)
  @max = max
  @current_attempt = 0

  @interval = opts.fetch(:interval, 1)
  @max_interval = opts.fetch(:max_interval, Float::INFINITY)
  @backoff_factor = opts.fetch(:backoff_factor, 1)

  @exceptions = opts.fetch(:exceptions) { [StandardError].freeze }
  @retry_block = opts.fetch(:retry_block, Proc.new {})
  @retry_if = opts.fetch(:retry_if, Proc.new { true })
end

Instance Method Details

#call(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ree_lib/packages/ree_std/package/ree_std/retry.rb', line 33

def call(&block)
  block.call
rescue => e
  raise unless match_error?(e)
  raise unless has_attempts?

  @retry_block.call(@current_attempt, e)

  Kernel.sleep(calculate_retry_interval)

  increment_attemt!
  retry
end