Sha256: ffaf8433144e9ad7d7b1c06a529c1ece3e88d2ee2fe00ee792ae0d65936cb84f

Contents?: true

Size: 1.17 KB

Versions: 73

Compression:

Stored size: 1.17 KB

Contents

module Tins
  module Attempt
    def attempt(opts = {}, &block)
      sleep           = nil
      exception_class = StandardError
      if Numeric === opts
        attempts = opts
      else
        attempts        = opts[:attempts] || 1
        exception_class = opts[:exception_class] if opts.key?(:exception_class)
        sleep           = opts[:sleep]
        reraise         = opts[:reraise]
      end
      return if attempts <= 0
      count = 0
      if exception_class.nil?
        begin
          count += 1
          if block.call(count)
            return true
          elsif count < attempts
            sleep_duration(sleep, count)
          end
        end until count == attempts
        false
      else
        begin
          count += 1
          block.call(count)
          true
        rescue exception_class
          if count < attempts
            sleep_duration(sleep, count)
            retry
          end
          reraise ? raise : false
        end
      end
    end

    private

    def sleep_duration(duration, count)
      case duration
      when Numeric
        sleep duration
      when Proc
        sleep duration.call(count)
      end
    end
  end
end

Version data entries

73 entries across 71 versions & 7 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/attempt.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/attempt.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/lib/tins/attempt.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/lib/tins/attempt.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/attempt.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/attempt.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/attempt.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/attempt.rb
suzuko-0.1.8 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/lib/tins/attempt.rb
suzuko-0.1.7 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/lib/tins/attempt.rb
mastermind_adeybee-0.1.4 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/attempt.rb
mastermind_adeybee-0.1.3 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/attempt.rb
mastermind_adeybee-0.1.2 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/attempt.rb
mastermind_adeybee-0.1.1 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/attempt.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/tins-1.6.0/lib/tins/attempt.rb
tins-1.6.0 lib/tins/attempt.rb
tins-1.5.4 lib/tins/attempt.rb
tins-1.5.3 lib/tins/attempt.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/tins-1.5.1/lib/tins/attempt.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/tins-1.5.2/lib/tins/attempt.rb