Class: Translatomatic::RetryExecutor

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/retry_executor.rb

Overview

Executes code with retry on exceptions

Direct Known Subclasses

HTTP::Client::HttpRetryExecutor

Instance Method Summary collapse

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(options = {})
  @max_retries = options[:max_retries] || DEFAULT_RETRIES
  @retriable = options[:retriable] || [StandardError]
  @delay = options[:retry_delay]
end

Instance Method Details

#retriable?(exception) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/translatomatic/retry_executor.rb', line 33

def retriable?(exception)
  @retriable.any? { |i| exception.kind_of?(i) }
end

#runObject

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.

Returns:

  • (Object)

    the return value of the block



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