class PerfectRetry

Constants

DEFAULTS
REGISTERED_CONFIG
VERSION

Attributes

config[R]

Public Class Methods

deregister_all() click to toggle source
# File lib/perfect_retry.rb, line 39
def self.deregister_all
  REGISTERED_CONFIG.clear
end
disable!() click to toggle source
# File lib/perfect_retry.rb, line 43
def self.disable!
  @disabled = true
end
disabled?() click to toggle source
# File lib/perfect_retry.rb, line 51
def self.disabled?
  @disabled.nil? ? false : @disabled
end
enable!() click to toggle source
# File lib/perfect_retry.rb, line 47
def self.enable!
  @disabled = false
end
new(config_key = nil, &block) click to toggle source
# File lib/perfect_retry.rb, line 57
def initialize(config_key = nil, &block)
  @config = REGISTERED_CONFIG[config_key] || default_config
  block.call(@config) if block_given?
  @config.set_log_level
end
register(name, &block) click to toggle source
# File lib/perfect_retry.rb, line 25
def self.register(name, &block)
  config = Config.create_from_hash(DEFAULTS)
  block.call(config)
  REGISTERED_CONFIG[name] = config
end
registered_config(key) click to toggle source
# File lib/perfect_retry.rb, line 35
def self.registered_config(key)
  REGISTERED_CONFIG[key]
end
registered_config_all() click to toggle source
# File lib/perfect_retry.rb, line 31
def self.registered_config_all
  REGISTERED_CONFIG
end
with_retry(config_key = nil, &block) click to toggle source
# File lib/perfect_retry.rb, line 21
def self.with_retry(config_key = nil, &block)
  new(config_key).with_retry(&block)
end

Public Instance Methods

default_config() click to toggle source
# File lib/perfect_retry.rb, line 63
def default_config
  Config.create_from_hash(DEFAULTS)
end
should_retry?(count) click to toggle source
# File lib/perfect_retry.rb, line 105
def should_retry?(count)
  return true unless config.limit
  count < config.limit
end
sleep_before_retry(count) click to toggle source
# File lib/perfect_retry.rb, line 101
def sleep_before_retry(count)
  sleep config.sleep_sec(count)
end
with_retry(&block) click to toggle source
# File lib/perfect_retry.rb, line 67
def with_retry(&block)
  return block.call(0) if PerfectRetry.disabled?

  count = 0
  begin
    retry_with_catch(count, &block)
  rescue *config.dont_rescues => e
    raise e
  rescue *config.rescues => e
    e.backtrace.each do |line|
      config.logger.debug line
    end

    if should_retry?(count)
      count += 1
      config.logger.warn "[#{count}/#{config.limit || "Infinitiy"}] Retrying after #{config.sleep_sec(count)} seconds. Ocurred: #{e}(#{e.class})"
      sleep_before_retry(count)
      retry
    end

    if config.raise_original_error
      raise e
    else
      error = TooManyRetry.new("too many retry (#{config.limit} times)")
      if config.prefer_original_backtrace
        error.set_backtrace(e.backtrace)
      end
      raise error
    end
  ensure
    config.ensure.call
  end
end

Private Instance Methods

retry_with_catch(count, &block) click to toggle source
# File lib/perfect_retry.rb, line 112
def retry_with_catch(count, &block)
  proc do
    catch(:retry) do
      return block.call(count)
    end

    redo # reached here only `throw :retry` called in a block
  end.call
end