class Reattempt::Retry

Retry the loop iterator if configured caught exceptions are raised and retry count is not exceeded, sleeping as per a given backoff configuration.

Example: “`ruby bo = Reattempt::Backoff.new(min_delay: 0.1, max_delay: 1.0, jitter: 0.5) try = Reattempt::Retry.new(tries: 5, rescue: TempError, backoff: bo) begin

try.each do |attempt|
  raise TempError, "Failed in attempt #{attempt}"
end

rescue Reattempt::RetriesExceeded => e

p e.cause # => #<TempError: "Failed in attempt 5">

end “`

Public Instance Methods

each() { |try| ... } click to toggle source

Yield the block with the current attempt number, starting from 1, for up to tries times. Setting tries to zero will result in an instant RetriesExceeded, which may be useful for testing.

If any of the configured rescue exceptions are raised (as matched by +===+), call rescue_proc with the exception, call sleep_proc with the delay as configured by backoff, and try again up to retries times.

rescue_proc defaults to a no-op.

sleep_proc defaults to +Kernel#sleep+.

Raise RetriesExceeded when the count is exceeded, setting cause to the previous exception.

# File lib/reattempt.rb, line 128
def each
  return enum_for(:each) unless block_given?

  ex = nil

  backoff.lazy.take(tries).each_with_index do |delay, try|
    return yield(try + 1)
  rescue Exception => ex
    raise unless self.rescue.any? { |r| r === ex }
    rescue_proc.call ex
    sleep_proc.call delay
  end

  raise RetriesExceeded, cause: ex
end