Class: MercadoPublicoCl::Retryer

Inherits:
Object
  • Object
show all
Defined in:
lib/mercado_publico_cl/retryer.rb

Constant Summary collapse

RETRYABLE =
{
  RateLimitError => :rate_limit,
  TimeoutError => :timeout,
  ApiError => :server_error
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Retryer

Returns a new instance of Retryer.



13
14
15
# File 'lib/mercado_publico_cl/retryer.rb', line 13

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



11
12
13
# File 'lib/mercado_publico_cl/retryer.rb', line 11

def configuration
  @configuration
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mercado_publico_cl/retryer.rb', line 17

def call
  attempts = 0
  max_attempts = configuration.max_retries.to_i + 1

  begin
    attempts += 1
    yield
  rescue *RETRYABLE.keys => e
    raise e unless retryable?(e) && attempts < max_attempts

    wait = wait_for(e, attempts)
    log_retry(e, attempts, max_attempts, wait)
    sleep(wait)
    retry
  end
end