Class: Mongo::Server::Monitor

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/mongo/server/monitor.rb,
lib/mongo/server/monitor/connection.rb

Overview

This object is responsible for keeping server status up to date, running in a separate thread as to not disrupt other operations.

Since:

  • 2.0.0

Defined Under Namespace

Classes: Connection

Constant Summary collapse

HEARTBEAT_FREQUENCY =

The default time for a server to refresh its status is 10 seconds.

Since:

  • 2.0.0

10.freeze
MIN_SCAN_FREQUENCY =

The minimum time between forced server scans. Is minHeartbeatFrequencyMS in the SDAM spec.

Since:

  • 2.0.0

0.5.freeze
RTT_WEIGHT_FACTOR =

The weighting factor (alpha) for calculating the average moving round trip time.

Since:

  • 2.0.0

0.2.freeze

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(address, listeners, options = {}) ⇒ Monitor

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:

Monitor must never be directly instantiated outside of a Server.

Create the new server monitor.

Examples:

Create the server monitor.

Mongo::Server::Monitor.new(address, listeners)

Parameters:

  • address (Address)

    The address to monitor.

  • listeners (Event::Listeners)

    The event listeners.

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

    The options.

Since:

  • 2.0.0



96
97
98
99
100
101
102
103
# File 'lib/mongo/server/monitor.rb', line 96

def initialize(address, listeners, options = {})
  @description = Description.new(address, {})
  @inspector = Description::Inspector.new(listeners)
  @options = options.freeze
  @connection = Connection.new(address, options)
  @last_round_trip_time = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#connectionMongo::Connection (readonly)

Returns connection The connection to use.

Returns:

  • (Mongo::Connection)

    connection The connection to use.

Since:

  • 2.0.0



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

def connection
  @connection
end

#descriptionServer::Description (readonly)

Returns description The server description the monitor refreshes.

Returns:

Since:

  • 2.0.0



48
49
50
# File 'lib/mongo/server/monitor.rb', line 48

def description
  @description
end

#inspectorDescription::Inspector (readonly)

Returns inspector The description inspector.

Returns:

Since:

  • 2.0.0



51
52
53
# File 'lib/mongo/server/monitor.rb', line 51

def inspector
  @inspector
end

#optionsHash (readonly)

Returns options The server options.

Returns:

  • (Hash)

    options The server options.

Since:

  • 2.0.0



54
55
56
# File 'lib/mongo/server/monitor.rb', line 54

def options
  @options
end

Instance Method Details

#heartbeat_frequencyInteger

Get the refresh interval for the server. This will be defined via an option or will default to 5.

Examples:

Get the refresh interval.

server.heartbeat_frequency

Returns:

  • (Integer)

    The heartbeat frequency, in seconds.

Since:

  • 2.0.0



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

def heartbeat_frequency
  @heartbeat_frequency ||= options[:heartbeat_frequency] || HEARTBEAT_FREQUENCY
end

#restart!Thread

Restarts the server monitor unless the current thread is alive.

Examples:

Restart the monitor.

monitor.restart!

Returns:

  • (Thread)

    The thread the monitor runs on.

Since:

  • 2.1.0



144
145
146
# File 'lib/mongo/server/monitor.rb', line 144

def restart!
  @thread.alive? ? @thread : run!
end

#run!Thread

Runs the server monitor. Refreshing happens on a separate thread per server.

Examples:

Run the monitor.

monitor.run

Returns:

  • (Thread)

    The thread the monitor runs on.

Since:

  • 2.0.0



114
115
116
117
118
119
120
121
# File 'lib/mongo/server/monitor.rb', line 114

def run!
  @thread = Thread.new(heartbeat_frequency) do |i|
    loop do
      sleep(i)
      scan!
    end
  end
end

#scan!Description

Force the monitor to immediately do a check of its server.

Examples:

Force a scan.

monitor.scan!

Returns:

Since:

  • 2.0.0



64
65
66
67
# File 'lib/mongo/server/monitor.rb', line 64

def scan!
  throttle_scan_frequency!
  @description = inspector.run(description, *ismaster)
end

#stop!Boolean

Stops the server monitor. Kills the thread so it doesn’t continue taking memory and sending commands to the connection.

Examples:

Stop the monitor.

monitor.stop!

Returns:

  • (Boolean)

    Is the Thread stopped?

Since:

  • 2.0.0



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

def stop!
  connection.disconnect! && @thread.kill && @thread.stop?
end