Class: Loggie::Logentries::Retry

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/loggie/logentries/retry.rb

Overview

Allows a block to be retried a number of times up until MAX_RETRY Each retry will sleep RETRY_DELAY_SECONDS before requesting It checks the response and extracts the polling URI for progress

Defined Under Namespace

Classes: RetryCountExceededError, RetryError, RetryResponseError

Constant Summary collapse

MIN_ACCEPTED_SUCCESS_STATUS_CODE =
203

Instance Method Summary collapse

Methods included from Logging

included, logger, #logger, logger=

Constructor Details

#initialize(block: nil) ⇒ Retry

Returns a new instance of Retry.



15
16
17
18
19
20
# File 'lib/loggie/logentries/retry.rb', line 15

def initialize(block: nil)
  @retry_count = 0
  @max_retry = Loggie.configuration.max_retry
  @sleep_before_retry_seconds = Loggie.configuration.sleep_before_retry_seconds
  @user_block = block
end

Instance Method Details

#call(url, method, options, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/loggie/logentries/retry.rb', line 22

def call(url, method, options, &block)
  @retry_count ||= 0
  response = block.call(url, method, options)
  logger.debug "#{self.class} retry:#{@retry_count}, response:#{response.body}"

  if response.code.to_i > MIN_ACCEPTED_SUCCESS_STATUS_CODE
    raise RetryCountExceededError, "Failed request with:#{response.message}"
  end

  res = Response.new response
  return res.events if res.events?

  @retry_count += 1
  if @retry_count > max_retry
    raise RetryCountExceededError, "Retry count of #{max_retry} reached"
  end
  logger.debug "Logentries returned progress:#{res.progress}, retry_count:#{@retry_count}"
  user_block.call(res.progress, @retry_count) if user_block

  sleep sleep_before_retry_seconds

  self.call(res.next_url, :get, nil, &block)

rescue RetryError => e
  logger.info e.message
  []
end