class Sqreen::Kit::RetryPolicy

Constants

DEFAULT_FATAL_EXCEPTIONS
DEFAULT_RETRIES
DEFAULT_WAITS_S

Attributes

fatal_exceptions[R]
max_retries[R]
wait_s[R]

Public Class Methods

new(opts = {}) click to toggle source

@param opts the parameters of the retry policy @option opts [Integer] the maximum number of tries @option opts [Float] wait_s wait these seconds before a retry @option opts [Array<Class>] exception classes for which no retry will

be attempted, besides non-StandardError
# File lib/sqreen/kit/retry_policy.rb, line 24
def initialize(opts = {})
  @max_retries = opts[:max_retries] || DEFAULT_RETRIES
  @wait_s = opts[:wait_s] || DEFAULT_WAITS_S
  @fatal_exceptions = opts[:fatal_exceptions] || DEFAULT_FATAL_EXCEPTIONS
end

Public Instance Methods

execute() { || ... } click to toggle source
# File lib/sqreen/kit/retry_policy.rb, line 30
def execute
  attempt = 1
  begin
    yield
  rescue ::Exception => e # rubocop:disable Lint/RescueException
    logger.warn { "Error on attempt ##{attempt}: #{e.message}" }
    logger.debug { e.backtrace }
    if fatal?(e)
      logger.debug { "Not retrying after seeing exception #{e.class}" }
      raise
    end
    if attempt > max_retries
      logger.debug { "Not retrying anymore after #{attempt} attempts" }
      raise
    end

    logger.debug { "Will retry after #{wait_s} seconds" }
    sleep(wait_s) unless wait_s.zero?
    attempt += 1
    retry
  end
end

Private Instance Methods

fatal?(exception) click to toggle source
# File lib/sqreen/kit/retry_policy.rb, line 55
def fatal?(exception)
  !exception.is_a?(StandardError) ||
    fatal_exceptions.any? { |ec| exception.is_a?(ec) }
end