Sha256: cc307f8a5e0e7ba40a348ca88a3550a922a0b15ba4ed9f9548d569fcb7ffe9d3

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

module Misc
  def self.insist(times = 4, sleep = nil, msg = nil)
    sleep_array = nil

    try = 0
    begin
      begin
        yield
      rescue Exception
        if Array === times
          sleep_array = times
          times = sleep_array.length
          sleep = sleep_array.shift
        end

        if sleep.nil?
          sleep_array = ([0] + [0.001, 0.01, 0.1, 0.5] * (times / 3)).sort[0..times-1]
          sleep = sleep_array.shift
        end
        raise $!
      end
    rescue TryAgain
      sleep sleep
      retry
    rescue StopInsist
      raise $!.exception
    rescue Aborted, Interrupt
      if msg
        Log.warn("Not Insisting after Aborted: #{$!.message} -- #{msg}")
      else
        Log.warn("Not Insisting after Aborted: #{$!.message}")
      end
      raise $!
    rescue Exception
      Log.exception $! if ENV["RBBT_LOG_INSIST"] == 'true'
      if msg
        Log.warn("Insisting after exception: #{$!.class} #{$!.message} -- #{msg}")
      elsif FalseClass === msg
        nil
      else
        Log.warn("Insisting after exception:  #{$!.class} #{$!.message}")
      end

      if sleep and try > 0
        sleep sleep
        sleep = sleep_array.shift || sleep if sleep_array
      else
        Thread.pass
      end

      try += 1
      retry if try < times
      raise $!
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
scout-gear-7.3.0 lib/scout/misc/insist.rb
scout-gear-7.2.0 lib/scout/misc/insist.rb
scout-gear-7.1.0 lib/scout/misc/insist.rb
scout-gear-6.0.0 lib/scout/misc/insist.rb
scout-gear-5.2.0 lib/scout/misc/insist.rb
scout-gear-5.1.1 lib/scout/misc/insist.rb