Class: Hermes::Retryable

Inherits:
Object
  • Object
show all
Defined in:
lib/hermes/retryable.rb

Instance Method Summary collapse

Constructor Details

#initialize(times:, errors: [], before_retry: ->(_error) {}) ⇒ Retryable

Returns a new instance of Retryable.



6
7
8
9
10
# File 'lib/hermes/retryable.rb', line 6

def initialize(times:, errors: [], before_retry: ->(_error) {})
  @times = times
  @errors = errors
  @before_retry = before_retry
end

Instance Method Details

#performObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hermes/retryable.rb', line 12

def perform
  executed = 0
  begin
    executed += 1
    yield
  rescue *errors => error
    if executed < times
      before_retry.call(error)
      retry
    else
      raise error
    end
  end
end