Sha256: 6213e7071f671079673bceda6061ac7f70fa052efa22a478db6a7b6f16c12114

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8
module Moped
  # Provides the shared behaviour for retry failed operations.
  #
  # @since 2.0.0
  module Retryable

    private

    # Execute the provided block on the cluster and retry if the execution
    # fails.
    #
    # @api private
    #
    # @example Execute with retry.
    #   preference.with_retry(cluster) do
    #     cluster.with_primary do |node|
    #       node.refresh
    #     end
    #   end
    #
    # @param [ Cluster ] cluster The cluster.
    # @param [ Integer ] retries The number of times to retry.
    #
    # @return [ Object ] The result of the block.
    #
    # @since 2.0.0
    def with_retry(cluster, retries = cluster.max_retries, &block)
      begin
        block.call
      rescue Errors::ConnectionFailure, Errors::PotentialReconfiguration => e
        raise e if e.is_a?(Errors::PotentialReconfiguration) &&
          ! (e.message.include?("not master") || e.message.include?("Not primary"))

        if retries > 0
          Loggable.warn("  MOPED:", "Retrying connection attempt #{retries} more time(s), nodes is #{cluster.nodes.inspect}, seeds are #{cluster.seeds.inspect}, cluster is #{cluster.inspect}. Error backtrace is #{e.backtrace}.", "n/a")
          sleep(cluster.retry_interval)
          cluster.refresh
          with_retry(cluster, retries - 1, &block)
        else
          raise e
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/bundler/gems/moped-cf817ca58a85/lib/moped/retryable.rb
moped-2.0.7 lib/moped/retryable.rb