Class: Forked::RetryStrategies::ExponentialBackoffWithLimit

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

Instance Method Summary collapse

Constructor Details

#initialize(logger:, on_error:, backoff_factor: 2, limit: nil) ⇒ ExponentialBackoffWithLimit

Returns a new instance of ExponentialBackoffWithLimit.



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

def initialize(logger:, on_error:, backoff_factor: 2, limit: nil)
  @logger = logger
  @on_error = on_error
  @backoff_factor = backoff_factor
  @limit = limit || 8
end

Instance Method Details

#run(ready_to_stop, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forked/retry_strategies/exponential_backoff_with_limit.rb', line 11

def run(ready_to_stop, &block)
  tries = 0
  begin
    block.call
  rescue => e
    tries += 1

    @logger.error("#{e.class} #{e.message}")
    @on_error.call(e, tries)
    raise if tries > @limit

    sleep_seconds = @backoff_factor**tries
    sleep_seconds.times do
      ready_to_stop.call
      sleep 1
    end

    retry
  end
end