Class: Mongo::Server::Monitor
- Inherits:
-
Object
- Object
- Mongo::Server::Monitor
- Extended by:
- Forwardable
- 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.
Defined Under Namespace
Classes: Connection
Constant Summary collapse
- HEARTBEAT_FREQUENCY =
The default time for a server to refresh its status is 10 seconds.
10.freeze
- MIN_SCAN_FREQUENCY =
The minimum time between forced server scans. Is minHeartbeatFrequencyMS in the SDAM spec.
0.5.freeze
- RTT_WEIGHT_FACTOR =
The weighting factor (alpha) for calculating the average moving round trip time.
0.2.freeze
Constants included from Loggable
Instance Attribute Summary collapse
-
#connection ⇒ Mongo::Connection
readonly
Connection The connection to use.
-
#description ⇒ Server::Description
readonly
Description The server description the monitor refreshes.
-
#inspector ⇒ Description::Inspector
readonly
Inspector The description inspector.
-
#last_scan ⇒ Time
readonly
Last_scan The time of the last server scan.
-
#options ⇒ Hash
readonly
Options The server options.
Instance Method Summary collapse
-
#heartbeat_frequency ⇒ Integer
Get the refresh interval for the server.
-
#initialize(address, listeners, options = {}) ⇒ Monitor
constructor
private
Create the new server monitor.
-
#restart! ⇒ Thread
Restarts the server monitor unless the current thread is alive.
-
#run! ⇒ Thread
Runs the server monitor.
-
#scan! ⇒ Description
Perform a check of the server with throttling, and update the server’s description.
-
#stop! ⇒ Boolean
Stops the server monitor.
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.
Monitor must never be directly instantiated outside of a Server.
Create the new server monitor.
116 117 118 119 120 121 122 123 124 |
# File 'lib/mongo/server/monitor.rb', line 116 def initialize(address, listeners, = {}) @description = Description.new(address, {}) @inspector = Description::Inspector.new(listeners) = .freeze @connection = Connection.new(address, ) @last_round_trip_time = nil @last_scan = nil @mutex = Mutex.new end |
Instance Attribute Details
#connection ⇒ Mongo::Connection (readonly)
Returns connection The connection to use.
45 46 47 |
# File 'lib/mongo/server/monitor.rb', line 45 def connection @connection end |
#description ⇒ Server::Description (readonly)
Returns description The server description the monitor refreshes.
49 50 51 |
# File 'lib/mongo/server/monitor.rb', line 49 def description @description end |
#inspector ⇒ Description::Inspector (readonly)
Returns inspector The description inspector.
52 53 54 |
# File 'lib/mongo/server/monitor.rb', line 52 def inspector @inspector end |
#last_scan ⇒ Time (readonly)
Returns last_scan The time of the last server scan.
60 61 62 |
# File 'lib/mongo/server/monitor.rb', line 60 def last_scan @last_scan end |
#options ⇒ Hash (readonly)
Returns options The server options.
55 56 57 |
# File 'lib/mongo/server/monitor.rb', line 55 def end |
Instance Method Details
#heartbeat_frequency ⇒ Integer
Get the refresh interval for the server. This will be defined via an option or will default to 5.
98 99 100 |
# File 'lib/mongo/server/monitor.rb', line 98 def heartbeat_frequency @heartbeat_frequency ||= [:heartbeat_frequency] || HEARTBEAT_FREQUENCY end |
#restart! ⇒ Thread
Restarts the server monitor unless the current thread is alive.
165 166 167 |
# File 'lib/mongo/server/monitor.rb', line 165 def restart! @thread.alive? ? @thread : run! end |
#run! ⇒ Thread
Runs the server monitor. Refreshing happens on a separate thread per server.
135 136 137 138 139 140 141 142 |
# File 'lib/mongo/server/monitor.rb', line 135 def run! @thread = Thread.new(heartbeat_frequency) do |i| loop do sleep(i) scan! end end end |
#scan! ⇒ Description
If the system clock is set to a time in the past, this method can sleep for a very long time.
Perform a check of the server with throttling, and update the server’s description.
If the server was checked less than MIN_SCAN_FREQUENCY seconds ago, sleep until MIN_SCAN_FREQUENCY seconds have passed since the last check. Then perform the check which involves running isMaster on the server being monitored and updating the server description as a result.
84 85 86 87 |
# File 'lib/mongo/server/monitor.rb', line 84 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.
153 154 155 |
# File 'lib/mongo/server/monitor.rb', line 153 def stop! connection.disconnect! && @thread.kill && @thread.stop? end |