class Mongo::Server

Represents a single server on the server side that can be standalone, part of a replica set, or a mongos.

@since 2.0.0

Attributes

address[R]

@return [ String ] The configured address for the server.

cluster[R]

@return [ Cluster ] cluster The server cluster.

monitor[R]

@return [ Monitor ] monitor The server monitor.

monitoring[R]

@return [ Monitoring ] monitoring The monitoring.

options[R]

@return [ Hash ] The options hash.

Public Class Methods

finalize(monitor) click to toggle source

When the server is flagged for garbage collection, stop the monitor thread.

@example Finalize the object.

Server.finalize(monitor)

@param [ Server::Monitor ] monitor The server monitor.

@since 2.2.0

# File lib/mongo/server.rb, line 132
def self.finalize(monitor)
  proc { monitor.stop! }
end
new(address, cluster, monitoring, event_listeners, options = {}) click to toggle source

Instantiate a new server object. Will start the background refresh and subscribe to the appropriate events.

@api private

@example Initialize the server.

Mongo::Server.new('127.0.0.1:27017', cluster, monitoring, listeners)

@note Server must never be directly instantiated outside of a Cluster.

@param [ Address ] address The host:port address to connect to. @param [ Cluster ] cluster The cluster the server belongs to. @param [ Monitoring ] monitoring The monitoring. @param [ Event::Listeners ] event_listeners The event listeners. @param [ Hash ] options The server options.

@since 2.0.0

# File lib/mongo/server.rb, line 153
def initialize(address, cluster, monitoring, event_listeners, options = {})
  @address = address
  @cluster = cluster
  @monitoring = monitoring
  @options = options.freeze
  @monitor = Monitor.new(address, event_listeners, options)
  monitor.scan!
  monitor.run!
  ObjectSpace.define_finalizer(self, self.class.finalize(monitor))
end

Public Instance Methods

==(other) click to toggle source

Is this server equal to another?

@example Is the server equal to the other?

server == other

@param [ Object ] other The object to compare to.

@return [ true, false ] If the servers are equal.

@since 2.0.0

# File lib/mongo/server.rb, line 78
def ==(other)
  return false unless other.is_a?(Server)
  address == other.address
end
connectable?() click to toggle source

Determine if a connection to the server is able to be established and messages can be sent to it.

@example Is the server connectable?

server.connectable?

@return [ true, false ] If the server is connectable.

@since 2.1.0

# File lib/mongo/server.rb, line 104
def connectable?
  context.with_connection do |connection|
    connection.connectable?
  end
end
context() click to toggle source

Get a new context for this server in which to send messages.

@example Get the server context.

server.context

@return [ Mongo::Server::Context ] context The server context.

@since 2.0.0

# File lib/mongo/server.rb, line 91
def context
  Context.new(self)
end
disconnect!() click to toggle source

Disconnect the server from the connection.

@example Disconnect the server.

server.disconnect!

@return [ true ] Always tru with no exception.

@since 2.0.0

# File lib/mongo/server.rb, line 118
def disconnect!
  pool.disconnect!
  monitor.stop! and true
end
inspect() click to toggle source

Get a pretty printed server inspection.

@example Get the server inspection.

server.inspect

@return [ String ] The nice inspection string.

@since 2.0.0

# File lib/mongo/server.rb, line 172
def inspect
  "#<Mongo::Server:0x#{object_id} address=#{address.host}:#{address.port}>"
end
matches_tag_set?(tag_set) click to toggle source

Determine if the provided tags are a subset of the server's tags.

@example Are the provided tags a subset of the server's tags.

server.matches_tag_set?({ 'rack' => 'a', 'dc' => 'nyc' })

@param [ Hash ] tag_set The tag set to compare to the server's tags.

@return [ true, false ] If the provided tags are a subset of the server's tags.

@since 2.0.0

# File lib/mongo/server.rb, line 198
def matches_tag_set?(tag_set)
  tag_set.keys.all? do |k|
    tags[k] && tags[k] == tag_set[k]
  end
end
pool() click to toggle source

Get the connection pool for this server.

@example Get the connection pool for the server.

server.pool

@return [ Mongo::Pool ] The connection pool.

@since 2.0.0

# File lib/mongo/server.rb, line 184
def pool
  @pool ||= cluster.pool(self)
end
reconnect!() click to toggle source

Restart the server monitor.

@example Restart the server monitor.

server.reconnect!

@return [ true ] Always true.

@since 2.1.0

# File lib/mongo/server.rb, line 212
def reconnect!
  monitor.restart! and true
end