Class: Forked::RetryStrategies::ExponentialBackoff

Inherits:
Object
  • Object
show all
Defined in:
lib/forked/retry_strategies/exponential_backoff.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger:, on_error:, backoff_factor: 2) ⇒ ExponentialBackoff

Returns a new instance of ExponentialBackoff.



4
5
6
7
8
# File 'lib/forked/retry_strategies/exponential_backoff.rb', line 4

def initialize(logger:, on_error:, backoff_factor: 2)
  @logger = logger
  @on_error = on_error
  @backoff_factor = backoff_factor
end

Instance Method Details

#run(ready_to_stop, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/forked/retry_strategies/exponential_backoff.rb', line 10

def run(ready_to_stop, &block)
  tries = 0
  begin
    block.call
  rescue => e
    tries += 1
    sleep_seconds = @backoff_factor**tries
    @logger.error("#{e.class} #{e.message}")
    @on_error.call(e, tries)
    sleep_seconds.times do
      ready_to_stop.call
      sleep 1
    end
    retry
  end
end