class GlobalRegistryModels::Retryer

Attributes

attempt_number[RW]
exception_classes[RW]
max_number_of_attempts[RW]
sleepiness[RW]

Public Class Methods

new(*exception_classes, max_attempts: 7) click to toggle source
# File lib/global_registry_models/retryer.rb, line 6
def initialize(*exception_classes, max_attempts: 7)
  self.attempt_number = 1
  self.sleepiness = 0
  self.max_number_of_attempts = max_attempts
  self.exception_classes = exception_classes.presence || [StandardError]
end

Public Instance Methods

try() { || ... } click to toggle source
# File lib/global_registry_models/retryer.rb, line 13
def try
  yield
rescue => exception
  raise exception unless retry_exception?(exception)
  puts "Retryer rescued #{ exception.inspect } on attempt #{ self.attempt_number }, sleeping for #{ self.sleepiness} seconds."
  sleep sleepiness
  increase_sleepiness
  puts "Retryer attempting retry #{ self.attempt_number += 1 }.#{ ' This is the last attempt!' if last_attempt? }"
  retry
end

Private Instance Methods

increase_sleepiness() click to toggle source
# File lib/global_registry_models/retryer.rb, line 35
def increase_sleepiness
  return self.sleepiness = 1 if sleepiness <= 0
  self.sleepiness = [1000, (sleepiness * 2)].min
end
last_attempt?() click to toggle source
# File lib/global_registry_models/retryer.rb, line 30
def last_attempt?
  return false if max_number_of_attempts.nil?
  attempt_number >= max_number_of_attempts
end
retry_exception?(exception) click to toggle source
# File lib/global_registry_models/retryer.rb, line 26
def retry_exception?(exception)
  exception_classes.include?(exception.class) && !last_attempt?
end