class Translatomatic::RetryExecutor
Executes code with retry on exceptions
Constants
- DEFAULT_RETRIES
@private
Public Class Methods
new(options = {})
click to toggle source
# File lib/translatomatic/retry_executor.rb, line 9 def initialize(options = {}) @max_retries = options[:max_retries] || DEFAULT_RETRIES @retriable = options[:retriable] || [StandardError] @delay = options[:retry_delay] end
Public Instance Methods
retriable?(exception)
click to toggle source
# File lib/translatomatic/retry_executor.rb, line 33 def retriable?(exception) @retriable.any? { |i| exception.kind_of?(i) } end
run() { || ... }
click to toggle source
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.
@return [Object] the return value of the block
# 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