Class: Sqreen::Kit::RetryPolicy

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/sqreen/kit/retry_policy.rb

Constant Summary collapse

DEFAULT_RETRIES =
2
DEFAULT_WAITS_S =
3
DEFAULT_FATAL_EXCEPTIONS =
[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.

Parameters:

  • opts (defaults to: {})

    the parameters of the retry policy

Options Hash (opts):

  • the (Integer)

    maximum number of tries

  • wait_s (Float)

    wait these seconds before a retry

  • exception (Array<Class>)

    classes for which no retry will be attempted, besides non-StandardError



24
25
26
27
28
# 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

Instance Attribute Details

#fatal_exceptionsObject (readonly)

Returns the value of attribute fatal_exceptions.



17
18
19
# File 'lib/sqreen/kit/retry_policy.rb', line 17

def fatal_exceptions
  @fatal_exceptions
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



17
18
19
# File 'lib/sqreen/kit/retry_policy.rb', line 17

def max_retries
  @max_retries
end

#wait_sObject (readonly)

Returns the value of attribute wait_s.



17
18
19
# File 'lib/sqreen/kit/retry_policy.rb', line 17

def wait_s
  @wait_s
end

Instance Method Details

#executeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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