Class: Mongo::Server

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Monitoring::Publishable
Defined in:
lib/mongo/server.rb,
lib/mongo/server/monitor.rb,
lib/mongo/server/context.rb,
lib/mongo/server/connection.rb,
lib/mongo/server/connectable.rb,
lib/mongo/server/description.rb,
lib/mongo/server/connection_pool.rb,
lib/mongo/server/monitor/connection.rb,
lib/mongo/server/description/features.rb,
lib/mongo/server/connection_pool/queue.rb,
lib/mongo/server/description/inspector.rb,
lib/mongo/server/description/inspector/primary_elected.rb,
lib/mongo/server/description/inspector/member_discovered.rb,
lib/mongo/server/description/inspector/description_changed.rb,
lib/mongo/server/description/inspector/standalone_discovered.rb

Overview

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

Since:

  • 2.0.0

Defined Under Namespace

Modules: Connectable Classes: Connection, ConnectionPool, Context, Description, Monitor

Constant Summary

CONNECT_TIMEOUT =

The default time in seconds to timeout a connection attempt.

Since:

  • 2.4.3

10.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Monitoring::Publishable

#publish_command, #publish_event, #publish_sdam_event

Constructor Details

#initialize(address, cluster, monitoring, event_listeners, options = {}) ⇒ Server

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Server must never be directly instantiated outside of a Cluster.

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

Examples:

Initialize the server.

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

Parameters:

  • address (Address)

    The host:port address to connect to.

  • cluster (Cluster)

    The cluster the server belongs to.

  • monitoring (Monitoring)

    The monitoring.

  • event_listeners (Event::Listeners)

    The event listeners.

  • options (Hash) (defaults to: {})

    The server options.

Since:

  • 2.0.0



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/mongo/server.rb', line 168

def initialize(address, cluster, monitoring, event_listeners, options = {})
  @address = address
  @cluster = cluster
  @monitoring = monitoring
  @options = options.freeze
  publish_sdam_event(
    Monitoring::SERVER_OPENING,
    Monitoring::Event::ServerOpening.new(address, cluster.topology)
  )
  @monitor = Monitor.new(address, event_listeners, options.merge(app_metadata: cluster.))
  monitor.scan!
  monitor.run!
  ObjectSpace.define_finalizer(self, self.class.finalize(monitor))
end

Instance Attribute Details

#addressString (readonly)

Returns The configured address for the server.

Returns:

  • (String)

    The configured address for the server.

Since:

  • 2.0.0



33
34
35
# File 'lib/mongo/server.rb', line 33

def address
  @address
end

#clusterCluster (readonly)

Returns cluster The server cluster.

Returns:

  • (Cluster)

    cluster The server cluster.

Since:

  • 2.0.0



36
37
38
# File 'lib/mongo/server.rb', line 36

def cluster
  @cluster
end

#monitorMonitor (readonly)

Returns monitor The server monitor.

Returns:

  • (Monitor)

    monitor The server monitor.

Since:

  • 2.0.0



39
40
41
# File 'lib/mongo/server.rb', line 39

def monitor
  @monitor
end

#monitoringMonitoring (readonly)

Returns monitoring The monitoring.

Returns:

Since:

  • 2.0.0



45
46
47
# File 'lib/mongo/server.rb', line 45

def monitoring
  @monitoring
end

#optionsHash (readonly)

Returns The options hash.

Returns:

  • (Hash)

    The options hash.

Since:

  • 2.0.0



42
43
44
# File 'lib/mongo/server.rb', line 42

def options
  @options
end

Class Method Details

.finalize(monitor) ⇒ Object

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

Examples:

Finalize the object.

Server.finalize(monitor)

Parameters:

Since:

  • 2.2.0



147
148
149
# File 'lib/mongo/server.rb', line 147

def self.finalize(monitor)
  proc { monitor.stop! }
end

Instance Method Details

#==(other) ⇒ true, false

Is this server equal to another?

Examples:

Is the server equal to the other?

server == other

Parameters:

  • other (Object)

    The object to compare to.

Returns:

  • (true, false)

    If the servers are equal.

Since:

  • 2.0.0



91
92
93
94
# File 'lib/mongo/server.rb', line 91

def ==(other)
  return false unless other.is_a?(Server)
  address == other.address
end

#connectable?true, false

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

Examples:

Is the server connectable?

server.connectable?

Returns:

  • (true, false)

    If the server is connectable.

Since:

  • 2.1.0



119
120
121
122
123
# File 'lib/mongo/server.rb', line 119

def connectable?
  with_connection do |connection|
    connection.connectable?
  end
end

#contextMongo::Server::Context

Deprecated.

Will be removed in version 3.0

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

Examples:

Get the server context.

server.context

Returns:

Since:

  • 2.0.0



106
107
108
# File 'lib/mongo/server.rb', line 106

def context
  Context.new(self)
end

#disconnect!true

Disconnect the server from the connection.

Examples:

Disconnect the server.

server.disconnect!

Returns:

  • (true)

    Always tru with no exception.

Since:

  • 2.0.0



133
134
135
136
# File 'lib/mongo/server.rb', line 133

def disconnect!
  pool.disconnect!
  monitor.stop! and true
end

#handle_auth_failure!Object

Handle authentication failure.

Examples:

Handle possible authentication failure.

server.handle_auth_failure! do
  Auth.get(user).(self)
end

Returns:

  • (Object)

    The result of the block execution.

Raises:

Since:

  • 2.3.0



262
263
264
265
266
267
# File 'lib/mongo/server.rb', line 262

def handle_auth_failure!
  yield
rescue Auth::Unauthorized
  unknown!
  raise
end

#inspectString

Get a pretty printed server inspection.

Examples:

Get the server inspection.

server.inspect

Returns:

  • (String)

    The nice inspection string.

Since:

  • 2.0.0



191
192
193
# File 'lib/mongo/server.rb', line 191

def inspect
  "#<Mongo::Server:0x#{object_id} address=#{address.host}:#{address.port}>"
end

#matches_tag_set?(tag_set) ⇒ true, false

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

Examples:

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

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

Parameters:

  • tag_set (Hash)

    The tag set to compare to the server's tags.

Returns:

  • (true, false)

    If the provided tags are a subset of the server's tags.

Since:

  • 2.0.0



217
218
219
220
221
# File 'lib/mongo/server.rb', line 217

def matches_tag_set?(tag_set)
  tag_set.keys.all? do |k|
    tags[k] && tags[k] == tag_set[k]
  end
end

#poolMongo::Pool

Get the connection pool for this server.

Examples:

Get the connection pool for the server.

server.pool

Returns:

  • (Mongo::Pool)

    The connection pool.

Since:

  • 2.0.0



203
204
205
# File 'lib/mongo/server.rb', line 203

def pool
  @pool ||= cluster.pool(self)
end

#reconnect!true

Restart the server monitor.

Examples:

Restart the server monitor.

server.reconnect!

Returns:

  • (true)

    Always true.

Since:

  • 2.1.0



231
232
233
# File 'lib/mongo/server.rb', line 231

def reconnect!
  monitor.restart! and true
end

#with_connection(&block) ⇒ Object

Execute a block of code with a connection, that is checked out of the server's pool and then checked back in.

Examples:

Send a message with the connection.

server.with_connection do |connection|
  connection.dispatch([ command ])
end

Returns:

  • (Object)

    The result of the block execution.

Since:

  • 2.3.0



246
247
248
# File 'lib/mongo/server.rb', line 246

def with_connection(&block)
  pool.with_connection(&block)
end