Class: Mongo::Server

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo/server.rb,
lib/mongo/server/context.rb,
lib/mongo/server/monitor.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/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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



153
154
155
156
157
158
159
160
161
162
# 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

Instance Attribute Details

#addressString (readonly)

Returns The configured address for the server.

Returns:

  • (String)

    The configured address for the server.

Since:

  • 2.0.0



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

def address
  @address
end

#clusterCluster (readonly)

Returns cluster The server cluster.

Returns:

  • (Cluster)

    cluster The server cluster.

Since:

  • 2.0.0



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

def cluster
  @cluster
end

#monitorMonitor (readonly)

Returns monitor The server monitor.

Returns:

  • (Monitor)

    monitor The server monitor.

Since:

  • 2.0.0



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

def monitor
  @monitor
end

#monitoringMonitoring (readonly)

Returns monitoring The monitoring.

Returns:

Since:

  • 2.0.0



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

def monitoring
  @monitoring
end

#optionsHash (readonly)

Returns The options hash.

Returns:

  • (Hash)

    The options hash.

Since:

  • 2.0.0



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

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



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

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



78
79
80
81
# File 'lib/mongo/server.rb', line 78

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



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

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

#contextMongo::Server::Context

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

Examples:

Get the server context.

server.context

Returns:

Since:

  • 2.0.0



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

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



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

def disconnect!
  pool.disconnect!
  monitor.stop! and true
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



172
173
174
# 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) ⇒ 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



198
199
200
201
202
# 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

#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



184
185
186
# File 'lib/mongo/server.rb', line 184

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



212
213
214
# File 'lib/mongo/server.rb', line 212

def reconnect!
  monitor.restart! and true
end