Class: Translatomatic::RetryExecutor
- Inherits:
-
Object
- Object
- Translatomatic::RetryExecutor
- Includes:
- Util
- Defined in:
- lib/translatomatic/retry_executor.rb
Overview
Executes code with retry on exceptions
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RetryExecutor
constructor
A new instance of RetryExecutor.
- #retriable?(exception) ⇒ Boolean
-
#run ⇒ Object
Attempt to run a block of code up to retries times.
Constructor Details
#initialize(options = {}) ⇒ RetryExecutor
Returns a new instance of RetryExecutor.
9 10 11 12 13 |
# File 'lib/translatomatic/retry_executor.rb', line 9 def initialize( = {}) @max_retries = [:max_retries] || DEFAULT_RETRIES @retriable = [:retriable] || [StandardError] @delay = [:retry_delay] end |
Instance Method Details
#retriable?(exception) ⇒ Boolean
33 34 35 |
# File 'lib/translatomatic/retry_executor.rb', line 33 def retriable?(exception) @retriable.any? { |i| exception.kind_of?(i) } end |
#run ⇒ Object
Attempt to run a block of code up to retries times. Reraises the exception if the block fails retries times or if
a non-retriable exception was raised.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/translatomatic/retry_executor.rb', line 19 def run fail_count = 0 begin yield rescue StandardError => e fail_count += 1 if fail_count < @max_retries && retriable?(e) sleep @delay if @delay retry end raise e end end |