Sha256: b406631e0d63eba4a9473445bc967e7d915b7f1c40b929a87626ad7535ac55d6

Contents?: true

Size: 1.77 KB

Versions: 77

Compression:

Stored size: 1.77 KB

Contents

class Knj::Retry
  def self.try(args = {}, &block)
    raise "No block was given." if !block_given?
    
    args[:tries] = 3 if !args[:tries]
    tries = []
    error = nil
    
    args[:tries].to_i.downto(1) do |count|
      error = nil
      dobreak = false
      
      begin
        if args[:timeout]
          begin
            Timeout.timeout(args[:timeout]) do
              block.call
              dobreak = true
              break
            end
          rescue Timeout::Error => e
            doraise = e if count <= 1
            error = e
            sleep(args[:wait]) if args[:wait] and !doraise
          end
        else
          block.call
          dobreak = true
          break
        end
      rescue Exception => e
        if e.class == Interrupt
          raise e if !args.key?(:interrupt) or args[:interrupt]
        elsif e.class == SystemExit
          raise e if !args.key?(:exit) or args[:exit]
        elsif count <= 1 or (args.key?(:errors) and args[:errors].index(e.class) == nil)
          doraise = e
        elsif args.key?(:errors) and args[:errors].index(e.class) != nil
          #given error was in the :errors-array - do nothing. Maybe later it should be logged and returned in a stats-hash or something? - knj
        end
        
        error = e
        sleep(args[:wait]) if args[:wait] and !doraise
      end
      
      if doraise
        if args[:return_error]
          tries << {:error => error}
          return {
            :tries => tries,
            :result => false
          }
        else
          raise e
        end
      elsif error
        tries << {:error => error}
      end
      
      break if dobreak
    end
    
    res = true
    res = false if error
    
    return {
      :tries => tries,
      :result => res
    }
  end
end

Version data entries

77 entries across 77 versions & 1 rubygems

Version Path
knjrbfw-0.0.45 lib/knj/retry.rb
knjrbfw-0.0.44 lib/knj/retry.rb
knjrbfw-0.0.43 lib/knj/retry.rb
knjrbfw-0.0.42 lib/knj/retry.rb
knjrbfw-0.0.41 lib/knj/retry.rb
knjrbfw-0.0.40 lib/knj/retry.rb
knjrbfw-0.0.39 lib/knj/retry.rb
knjrbfw-0.0.38 lib/knj/retry.rb
knjrbfw-0.0.37 lib/knj/retry.rb
knjrbfw-0.0.36 lib/knj/retry.rb
knjrbfw-0.0.35 lib/knj/retry.rb
knjrbfw-0.0.34 lib/knj/retry.rb
knjrbfw-0.0.33 lib/knj/retry.rb
knjrbfw-0.0.32 lib/knj/retry.rb
knjrbfw-0.0.31 lib/knj/retry.rb
knjrbfw-0.0.30 lib/knj/retry.rb
knjrbfw-0.0.29 lib/knj/retry.rb
knjrbfw-0.0.28 lib/knj/retry.rb
knjrbfw-0.0.27 lib/knj/retry.rb
knjrbfw-0.0.26 lib/knj/retry.rb