Class: Maglev::ProviderCall

Inherits:
Object
  • Object
show all
Defined in:
lib/maglev/provider_call.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts: Maglev.configuration.provider_max_attempts, timeout: Maglev.configuration.provider_timeout) ⇒ ProviderCall

Returns a new instance of ProviderCall.



12
13
14
15
# File 'lib/maglev/provider_call.rb', line 12

def initialize(max_attempts: Maglev.configuration.provider_max_attempts, timeout: Maglev.configuration.provider_timeout)
  @max_attempts = max_attempts
  @timeout = timeout
end

Instance Method Details

#call(operation:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/maglev/provider_call.rb', line 17

def call(operation:)
  attempt = 0

  begin
    attempt += 1
    Timeout.timeout(@timeout) { yield }
  rescue Timeout::Error => error
    retryable_error = RetryableProviderError.new(error.message)
    retryable_error.set_backtrace(error.backtrace)
    raise retryable_error if attempt >= @max_attempts

    instrument_retry(operation, attempt, retryable_error)
    retry
  rescue RetryableProviderError => error
    raise if attempt >= @max_attempts

    instrument_retry(operation, attempt, error)
    retry
  end
end