module Retryable

Constants

EXPONENTIAL_BACKOFF
LINEAR_BACKOFF
NO_BACKOFF

Public Instance Methods

retryable(tries = 1, on: [RuntimeError], backoff: NO_BACKOFF) { || ... } click to toggle source
# File lib/decoratable/retryable.rb, line 10
def retryable(tries = 1, on: [RuntimeError], backoff: NO_BACKOFF)
  attempts = 0
  on = Array(on)

  begin
    yield
  rescue *on

    if attempts >= tries
      raise
    else
      sleep backoff.call(attempts)
      attempts += 1
      retry
    end
  end
end