Sha256: 51545f914312cc6a759697e87696a26d765b34149c4ecbad85b6379967944f6d

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module Vx
  module ContainerConnector
    module Retriable
      # This will catch any exception and retry twice (three tries total):
      #   with_retries { ... }
      #
      # This will catch any exception and retry four times (five tries total):
      #   with_retries(:limit => 5) { ... }
      #
      # This will catch a specific exception and retry once (two tries total):
      #   with_retries(Some::Error, :limit => 2) { ... }
      #
      # You can also sleep inbetween tries. This is helpful if you're hoping
      # that some external service recovers from its issues.
      #   with_retries(Service::Error, :sleep => 1) { ... }
      #
      def with_retries(*args, &block)
        options = args.last.is_a?(Hash) ? args.pop : {}
        exceptions = args

        options[:limit] ||= 3
        options[:sleep] ||= 0
        exceptions = [Exception] if exceptions.empty?

        retried = 0
        begin
          yield
        rescue *exceptions => e
          $stderr.puts "got: #{inspect}, attempt #{retried}"
          if retried + 1 < options[:limit]
            retried += 1
            sleep options[:sleep]
            retry
          else
            raise e
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vx-container_connector-0.2.9 lib/vx/container_connector/mixin/retriable.rb
vx-container_connector-0.2.8 lib/vx/container_connector/mixin/retriable.rb
vx-container_connector-0.2.7 lib/vx/container_connector/mixin/retriable.rb