module Retriable

Constants

DEFAULT_ERROR_CLASSES

These errors will be handled automatically

Public Instance Methods

retry_with(opts = {}) { || ... } click to toggle source
# File lib/ec_utils/retriable.rb, line 12
def retry_with(opts = {}, &blk)
  raise('Block is required') if blk.nil?

  classes     = [opts[:errors] || DEFAULT_ERROR_CLASSES].flatten
  attempts    = opts[:attempts] || 3
  delay       = opts[:delay] || 1
  delay_inc   = opts[:increment_delay] == true
  delay_sleep = opts[:sleep] == true
  debug       = opts[:debug] == true

  # We need to handle our own retry method
  classes << Retriable::RetryError

  1.upto(attempts) do |i|
    begin
      puts "Trying #{i} attempt..." if debug
      yield
      puts 'Success' if debug
      return
    rescue Exception => err
      puts "Got an error on #{i} attemp: #{err}" if debug

      if (classes & err.class.ancestors).any?
        delay *= i if delay_inc
        sleep(delay) if delay_sleep
      else
        puts "Unhanded retriable error: #{err}" if debug
        raise(err)
      end
    end
  end

  raise "Retry attempts are exhausted (#{attempts} total)"
end
try_again() click to toggle source
# File lib/ec_utils/retriable.rb, line 47
def try_again
  raise(RetryError)
end