class Undertaker::Undertaker

Attributes

attempts[RW]
limit[R]
retry_condition[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/undertaker.rb, line 14
def initialize(options = {})
  @exceptions = []
  @attempts = 0
  @limit = options[:limit] || 10
  @logger = options[:logger] || ActiveSupport::Logger.new(STDOUT)
end

Public Instance Methods

execute() { || ... } click to toggle source
# File lib/undertaker.rb, line 21
def execute
  setup_execution
  yield
rescue StandardError => exception
  add_exception exception
  if should_retry?
    setup_retry
    retry
  end
  raise exception
end
retry_when(&block) click to toggle source
# File lib/undertaker.rb, line 33
def retry_when(&block)
  self.retry_condition = block
end

Private Instance Methods

add_exception(exception) click to toggle source
# File lib/undertaker.rb, line 48
def add_exception(exception)
  @exceptions << exception
end
exponential_backoff() click to toggle source
# File lib/undertaker.rb, line 64
def exponential_backoff
  (1.0 / 2.0 * (2.0**attempts - 1.0)).ceil
end
last_exception() click to toggle source
# File lib/undertaker.rb, line 52
def last_exception
  @exceptions.last
end
log_retry() click to toggle source
# File lib/undertaker.rb, line 68
def log_retry
  @logger.warn "Undertaker: #{last_exception} raised retring..."
  @logger.warn last_exception.backtrace[0..5].join("\n")
end
retry_condition?() click to toggle source
# File lib/undertaker.rb, line 60
def retry_condition?
  retry_condition.nil? || retry_condition.call(last_exception)
end
setup_execution() click to toggle source
# File lib/undertaker.rb, line 39
def setup_execution
  self.attempts += 1
end
setup_retry() click to toggle source
# File lib/undertaker.rb, line 43
def setup_retry
  log_retry
  sleep exponential_backoff
end
should_retry?() click to toggle source
# File lib/undertaker.rb, line 56
def should_retry?
  attempts < limit && retry_condition?
end