Class: Moped::ReadPreference::PrimaryPreferred

Inherits:
Object
  • Object
show all
Includes:
Selectable
Defined in:
lib/moped/read_preference/primary_preferred.rb

Overview

Encapsulates behaviour around a primary preferred read preference.

Since:

  • 2.0.0

Instance Attribute Summary

Attributes included from Selectable

#tags

Instance Method Summary collapse

Methods included from Selectable

#initialize, #query_options

Instance Method Details

#nameSymbol

Get the name for the read preference on the server side.

Examples:

Get the name of the read preference.

primary_preferred.name

Returns:

  • (Symbol)

    :primaryPreferred.

Since:

  • 2.0.0



19
20
21
# File 'lib/moped/read_preference/primary_preferred.rb', line 19

def name
  :primaryPreferred
end

#with_node(cluster, &block) ⇒ Object

Note:

If tag sets are provided then secondary with_nodeion will need to match the provided tags.

Select a primary node from the cluster. If no primary node is available then attempt to select a secondary. If no secondary is available then an exception will be raised.

Examples:

Prefer to with_node a primary node from the cluster.

preference.with_node(cluster) do |node|
  node.command(ismaster: 1)
end

Parameters:

  • cluster (Cluster)

    The cluster of nodes to select from.

  • block (Proc)

    The block to execute on the node.

Returns:

  • (Object)

    The result of the block.

Raises:

Since:

  • 2.0.0



44
45
46
47
48
49
50
51
52
# File 'lib/moped/read_preference/primary_preferred.rb', line 44

def with_node(cluster, &block)
  with_retry(cluster) do
    begin
      cluster.with_primary(&block)
    rescue Errors::ConnectionFailure
      cluster.with_secondary(&block)
    end
  end
end