Sha256: fabda7d911f137877e39243b5a83d8d1cf73a87b42c4f4f050a58182f18aa2b9
Contents?: true
Size: 1.81 KB
Versions: 6
Compression:
Stored size: 1.81 KB
Contents
# typed: ignore # Copyright (c) 2015 Sqreen. All Rights Reserved. # Please refer to our terms for more information: https://www.sqreen.com/terms.html require 'sqreen/kit/loggable' module Sqreen module Kit class RetryPolicy include Loggable DEFAULT_RETRIES = 2 DEFAULT_WAITS_S = 3 DEFAULT_FATAL_EXCEPTIONS = [].freeze attr_reader :max_retries, :wait_s, :fatal_exceptions # @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 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 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 def fatal?(exception) !exception.is_a?(StandardError) || fatal_exceptions.any? { |ec| exception.is_a?(ec) } end end end end
Version data entries
6 entries across 6 versions & 1 rubygems