Module: Ferrum::Utils::Attempt

Defined in:
lib/ferrum/utils/attempt.rb

Overview

A retry-with-backoff helper for re-running a block a fixed number of times when it raises one of a given set of exceptions, sleeping between attempts.

Class Method Summary collapse

Class Method Details

.with_retry(errors:, max:, wait:) ⇒ Object

Retries the block up to max times when one of errors is raised, sleeping wait seconds between attempts.

Parameters:

  • errors (Array<Class>, Class)

    Exception classes that trigger a retry.

  • max (Integer)

    Maximum number of attempts.

  • wait (Numeric)

    Seconds to sleep between attempts.

Returns:

  • (Object)


28
29
30
31
32
33
34
35
36
37
# File 'lib/ferrum/utils/attempt.rb', line 28

def with_retry(errors:, max:, wait:)
  attempts ||= 1
  yield
rescue *Array(errors)
  raise if attempts >= max

  attempts += 1
  sleep(wait)
  retry
end