Class: DeadBro::Monitor
- Inherits:
-
Object
- Object
- DeadBro::Monitor
- Defined in:
- lib/dead_bro/monitor.rb
Constant Summary collapse
- SLEEP_INTERVAL_SECONDS =
60
Instance Method Summary collapse
-
#initialize(client: DeadBro.client) ⇒ Monitor
constructor
A new instance of Monitor.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
Instance Method Details
#start ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dead_bro/monitor.rb', line 15 def start # Live thread already running — nothing to do. return if @running && @thread&.alive? # Reset: handles post-fork where @running=true but the thread is dead. @running = false return unless DeadBro.configuration.enabled @running = true @thread = Thread.new do Thread.current.abort_on_exception = false # Fetch initial settings before the first collection tick so processes # that haven't yet posted any metrics (e.g. Sidekiq at boot) still get # monitor_enabled and other remote settings from the backend. begin @client.post_heartbeat(sync: true) rescue => e log_error("Error fetching initial settings: #{e.}") end loop do break unless @running begin collect_and_send_stats rescue => e log_error("Error collecting stats: #{e.}") end # Interruptible sleep — stop() signals the CV so shutdown doesn't # block up to a full minute. Still naps the full interval during # normal operation. @stop_mutex.synchronize do @stop_cv.wait(@stop_mutex, SLEEP_INTERVAL_SECONDS) if @running end end end @thread end |
#stop ⇒ Object
58 59 60 61 62 63 |
# File 'lib/dead_bro/monitor.rb', line 58 def stop @running = false @stop_mutex.synchronize { @stop_cv.broadcast } @thread&.join(5) # Safety timeout in case the thread is mid-flight @thread = nil end |