class Bundler::Retry - bundler-1.6.9 Documentation

# class Bundler::Retry

General purpose class for retrying code that may fail

### Constants

DEFAULT_ATTEMPTS

### Attributes

attempts[RW]
current_run[RW]
name[RW]
total_runs[RW]

### Public Class Methods

new(name, exceptions = nil, attempts = nil) click to toggle source

    # File lib/bundler/retry.rb, line 11
    def initialize(name, exceptions = nil, attempts = nil)
      @name        = name
      attempts    ||= default_attempts
      @exceptions = Array(exceptions) || []
      @total_runs =  attempts.next # will run once, then upto attempts.times
    end

### Public Instance Methods

attempt(&block) click to toggle source

    # File lib/bundler/retry.rb, line 23
    def attempt(&block)
      @current_run = 0
      @failed      = false
      @error       = nil
      while keep_trying? do
        run(&block)
      end
      @result
    end

Also aliased as: attempts
attempts(&block)
Alias for: attempt
default_attempts() click to toggle source

    # File lib/bundler/retry.rb, line 18
    def default_attempts
      return Integer(self.class.attempts) if self.class.attempts
      DEFAULT_ATTEMPTS
    end

### Private Instance Methods

fail(e) click to toggle source

    # File lib/bundler/retry.rb, line 43
    def fail(e)
      @failed = true
      raise e if last_attempt? || @exceptions.any?{ |k| e.is_a?(k) }
      return true unless name
      Bundler.ui.warn "Retrying#{" #{name}" if name} due to error (#{current_run.next}/#{total_runs}): #{e.class} #{e.message}"
    end

keep_trying?() click to toggle source

    # File lib/bundler/retry.rb, line 50
    def keep_trying?
      return true  if current_run.zero?
      return false if last_attempt?
      return true  if @failed
    end

last_attempt?() click to toggle source

    # File lib/bundler/retry.rb, line 56
    def last_attempt?
      current_run >= total_runs
    end

run(&block) click to toggle source

    # File lib/bundler/retry.rb, line 35
    def run(&block)
      @failed      = false
      @current_run += 1
      @result = block.call
    rescue => e
      fail(e)
    end