Class: Mongo::Server::Monitor
- Inherits:
-
Object
- Object
- Mongo::Server::Monitor
- 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
Force the monitor to immediately do a check of its server.
-
#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.
101 102 103 104 105 106 107 108 109 |
# File 'lib/mongo/server/monitor.rb', line 101 def initialize(address, listeners, = {}) @description = Description.new(address, {}) @inspector = Description::Inspector.new(listeners) @options = .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.
44 45 46 |
# File 'lib/mongo/server/monitor.rb', line 44 def connection @connection end |
#description ⇒ Server::Description (readonly)
Returns description The server description the monitor refreshes.
48 49 50 |
# File 'lib/mongo/server/monitor.rb', line 48 def description @description end |
#inspector ⇒ Description::Inspector (readonly)
Returns inspector The description inspector.
51 52 53 |
# File 'lib/mongo/server/monitor.rb', line 51 def inspector @inspector end |
#last_scan ⇒ Time (readonly)
Returns last_scan The time of the last server scan.
59 60 61 |
# File 'lib/mongo/server/monitor.rb', line 59 def last_scan @last_scan end |
#options ⇒ Hash (readonly)
Returns options The server options.
54 55 56 |
# File 'lib/mongo/server/monitor.rb', line 54 def @options 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.
83 84 85 |
# File 'lib/mongo/server/monitor.rb', line 83 def heartbeat_frequency @heartbeat_frequency ||= [:heartbeat_frequency] || HEARTBEAT_FREQUENCY end |
#restart! ⇒ Thread
Restarts the server monitor unless the current thread is alive.
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.
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.
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.
138 139 140 |
# File 'lib/mongo/server/monitor.rb', line 138 def stop! connection.disconnect! && @thread.kill && @thread.stop? end |