class HalClient::Retryinator

Example usage: Retryinator.call { fetch_http_response }

Constants

DEFAULT_DURATION
DEFAULT_MAX_TRIES

Attributes

duration[R]
logger[R]
max_tries[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/hal_client/retryinator.rb, line 19
def initialize(options={})
  @max_tries = options.fetch(:max_tries, DEFAULT_MAX_TRIES)
  @duration = options.fetch(:duration, DEFAULT_DURATION)
  @logger = options.fetch(:logger, HalClient::NullLogger.new)
end

Public Instance Methods

retryable() { |block| ... } click to toggle source
# File lib/hal_client/retryinator.rb, line 25
def retryable(&block)
  current_try = 1

  loop do
    begin
      result = yield block

      if server_error?(result.code)
        logger.warn "Received a #{result.code} response with body:\n#{result.body}"
        return result if current_try >= max_tries
      else
        return result
      end
    rescue HttpError => e
      logger.warn "Encountered an HttpError: #{e.message}"
      raise e if current_try >= max_tries
    end

    logger.warn "Failed attempt #{current_try} of #{max_tries}. " +
                  "Waiting #{duration} seconds before retrying"

    current_try += 1
    sleep duration
  end
end
server_error?(status_code) click to toggle source
# File lib/hal_client/retryinator.rb, line 51
def server_error?(status_code)
  500 <= status_code && status_code < 600
end