Sha256: 7507ae32b677e677a973052d9c44bb9e06e7f858cc3f7ac5ccbc0939c7ec5c39

Contents?: true

Size: 1.56 KB

Versions: 84

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8
module LogStash
  module Retryable
    # execute retryable code block
    # @param [Hash] options retryable options
    # @option options [Fixnum] :tries retries to perform, default 1, set to 0 for infinite retries. 1 means that upon exception the block will be retried once
    # @option options [Fixnum] :base_sleep seconds to sleep on first retry, default 1
    # @option options [Fixnum] :max_sleep max seconds to sleep upon exponential backoff, default 1
    # @option options [Exception] :rescue exception class list to retry on, defaults is Exception, which retries on any Exception.
    # @option options [Proc] :on_retry call the given Proc/lambda before each retry with the raised exception as parameter
    def retryable(options = {}, &block)
      options = {
        :tries => 1,
        :rescue => Exception,
        :on_retry => nil,
        :base_sleep => 1,
        :max_sleep => 1,
      }.merge(options)

      rescue_classes = Array(options[:rescue])
      max_sleep_retry = Math.log2(options[:max_sleep] / options[:base_sleep])
      retry_count = 0

      begin
        return yield(retry_count)
      rescue *rescue_classes => e
        raise e if options[:tries] > 0 && retry_count >= options[:tries]

        options[:on_retry].call(retry_count + 1, e) if options[:on_retry]

        # dont compute and maybe overflow exponent on too big a retry count
        seconds = retry_count < max_sleep_retry ? options[:base_sleep] * (2 ** retry_count) : options[:max_sleep]
        sleep(seconds)

        retry_count += 1
        retry
      end
    end
  end
end

Version data entries

84 entries across 84 versions & 7 rubygems

Version Path
logstash-output-scalyr-0.1.9 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-scalyr-0.1.8 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-scalyr-0.1.7 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-scalyr-0.1.6 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-newrelic-1.2.0 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-output-scalyr-0.1.2 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb
logstash-core-5.6.16-java lib/logstash/util/retryable.rb
logstash-core-5.6.15-java lib/logstash/util/retryable.rb
logstash-core-5.6.14-java lib/logstash/util/retryable.rb
logstash-core-5.6.13-java lib/logstash/util/retryable.rb
logstash-core-5.6.12-java lib/logstash/util/retryable.rb
logstash-core-5.6.11-java lib/logstash/util/retryable.rb
logstash-core-6.3.2-java lib/logstash/util/retryable.rb
logstash-core-6.3.1-java lib/logstash/util/retryable.rb
logstash-filter-device_detection-1.0.7-java vendor/bundle/jruby/1.9/gems/logstash-core-5.6.4-java/lib/logstash/util/retryable.rb