Sha256: cc2b21ee24e6d4173f71e44baf2cd1445879acacbca993d8324cdaef59d762cd

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

module ASIR
  # !SLIDE
  # Generic retry behavior
  module RetryBehavior
    # Maximum trys.
    attr_accessor :try_max
    # Initial amount of seconds to sleep between each try.
    attr_accessor :try_sleep
    # Amount of seconds to increment sleep between each try.
    attr_accessor :try_sleep_increment
    # Maxinum amount of seconds to sleep between each try.
    attr_accessor :try_sleep_max

    # Yields:
    #   :try, n_try
    #   :rescue, exc
    #   :retry, exc
    #   :failed, nil
    def with_retry
      n_try = 0
      sleep_secs = try_sleep
      result = done = last_exception = nil
      begin
        n_try += 1
        result = yield :try, n_try
        done = true
      rescue *Error::Unrecoverable.modules
        raise
      rescue *Error::Unforwardable.unforwardable
        raise
      rescue ::Exception => exc
        last_exception = exc
        yield :rescue, exc
        if ! try_max || try_max > n_try
          yield :retry, exc
          if sleep_secs
            sleep sleep_secs if sleep_secs > 0
            sleep_secs += try_sleep_increment if try_sleep_increment
            sleep_secs = try_sleep_max if try_sleep_max && sleep_secs > try_sleep_max
          end
          retry
        end
      end
      unless done
        unless yield :failed, last_exception
          exc = last_exception
          raise RetryError, "Retry failed: #{exc.inspect}", exc.backtrace
        end
      end
      result
    end
    class RetryError < Error; end
  end # module
  # !SLIDE END
end # module ASIR

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
asir-1.2.8 lib/asir/retry_behavior.rb
asir-1.2.7 lib/asir/retry_behavior.rb
asir-1.2.6 lib/asir/retry_behavior.rb
asir-1.2.5 lib/asir/retry_behavior.rb
asir-1.2.3 lib/asir/retry_behavior.rb
asir-1.2.2 lib/asir/retry_behavior.rb