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

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



101
102
103
104
105
106
107
108
109
# File 'lib/mongo/server/monitor.rb', line 101

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
  @last_scan = 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

#last_scanTime (readonly)

Returns last_scan The time of the last server scan.

Returns:

  • (Time)

    last_scan The time of the last server scan.

Since:

  • 2.4.0



59
60
61
# File 'lib/mongo/server/monitor.rb', line 59

def last_scan
  @last_scan
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



83
84
85
# File 'lib/mongo/server/monitor.rb', line 83

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



150
151
152
# File 'lib/mongo/server/monitor.rb', line 150

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



120
121
122
123
124
125
126
127
# File 'lib/mongo/server/monitor.rb', line 120

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



69
70
71
72
# File 'lib/mongo/server/monitor.rb', line 69

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



138
139
140
# File 'lib/mongo/server/monitor.rb', line 138

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