Class: Sqreen::Kit::RetryPolicy
- Inherits:
-
Object
- Object
- Sqreen::Kit::RetryPolicy
- 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
-
#fatal_exceptions ⇒ Object
readonly
Returns the value of attribute fatal_exceptions.
-
#max_retries ⇒ Object
readonly
Returns the value of attribute max_retries.
-
#wait_s ⇒ Object
readonly
Returns the value of attribute wait_s.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(opts = {}) ⇒ RetryPolicy
constructor
A new instance of RetryPolicy.
Constructor Details
#initialize(opts = {}) ⇒ RetryPolicy
Returns a new instance of RetryPolicy.
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_exceptions ⇒ Object (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_retries ⇒ Object (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_s ⇒ Object (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
#execute ⇒ Object
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.}" } 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 |