Class: Userlist::Retryable

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/userlist/retryable.rb

Constant Summary collapse

RETRIES =
10
DELAY =
100
MULTIPLIER =
2
MAX_DELAY =
10_000

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(retries: RETRIES, delay: DELAY, max_delay: MAX_DELAY, multiplier: MULTIPLIER, &retry_check) ⇒ Retryable

Returns a new instance of Retryable.



10
11
12
13
14
15
16
# File 'lib/userlist/retryable.rb', line 10

def initialize(retries: RETRIES, delay: DELAY, max_delay: MAX_DELAY, multiplier: MULTIPLIER, &retry_check)
  @retries = retries
  @delay = delay
  @max_delay = max_delay
  @multiplier = multiplier
  @retry_check = retry_check
end

Instance Method Details

#attemptObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/userlist/retryable.rb', line 22

def attempt
  (0..@retries).each do |attempt|
    if attempt.positive?
      milliseconds = delay(attempt)
      logger.debug "Retrying in #{milliseconds}ms, #{@retries - attempt} retries left"
      sleep(milliseconds / 1000.0)
    end

    result = yield

    return result unless retry?(result)
  end

  logger.debug 'Retries exhausted, giving up'

  nil
end

#retry?(value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/userlist/retryable.rb', line 18

def retry?(value)
  @retry_check.call(value)
end