Class: Async::Service::Supervisor::ProcessMonitor

Inherits:
Monitor
  • Object
show all
Defined in:
lib/async/service/supervisor/process_monitor.rb,
lib/metrics/provider/async/service/supervisor/process_monitor.rb

Overview

Monitors process metrics and logs them periodically.

Uses the ‘process-metrics` gem to capture CPU and memory metrics for a process tree. Unlike MemoryMonitor, this monitor captures metrics for the entire process tree by tracking the parent process ID (ppid), which is more efficient than tracking individual processes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Monitor

#run, #status, #to_json

Constructor Details

#initialize(interval: 60, ppid: nil) ⇒ ProcessMonitor

Create a new process monitor.



23
24
25
26
# File 'lib/async/service/supervisor/process_monitor.rb', line 23

def initialize(interval: 60, ppid: nil)
	super(interval: interval)
	@ppid = ppid || Process.ppid
end

Instance Attribute Details

#ppidObject (readonly)

Returns the value of attribute ppid.



29
30
31
# File 'lib/async/service/supervisor/process_monitor.rb', line 29

def ppid
  @ppid
end

#The parent process ID being monitored.(parentprocessIDbeingmonitored.) ⇒ Object (readonly)



29
# File 'lib/async/service/supervisor/process_monitor.rb', line 29

attr :ppid

Class Method Details

.monitor_typeObject

The key used when this monitor’s status is aggregated with others.



61
62
63
# File 'lib/async/service/supervisor/process_monitor.rb', line 61

def self.monitor_type
	:process_monitor
end

Instance Method Details

#as_jsonObject

Serialize process metrics for JSON.



66
67
68
# File 'lib/async/service/supervisor/process_monitor.rb', line 66

def as_json
	{ppid: @ppid, metrics: self.metrics}
end

#emit(metrics) ⇒ Object

Emit the process metrics.



73
74
75
76
77
78
# File 'lib/async/service/supervisor/process_monitor.rb', line 73

def emit(metrics)
	# Log each process individually for better searchability in log platforms:
	metrics.each do |process_id, general|
		Console.info(self, "Process metrics captured.", general: general)
	end
end

#metricsObject

Capture current process metrics for the entire process tree.



56
57
58
# File 'lib/async/service/supervisor/process_monitor.rb', line 56

def metrics
	Process::Metrics::General.capture(ppid: @ppid).transform_values!(&:as_json)
end

#register(supervisor_controller) ⇒ Object

Register a worker with the process monitor.

This is provided for consistency with MemoryMonitor, but since we monitor the entire process tree via ppid, we don’t need to track individual workers.



37
38
39
40
# File 'lib/async/service/supervisor/process_monitor.rb', line 37

def register(supervisor_controller)
	process_id = supervisor_controller.process_id
	Console.debug(self, "Worker registered.", supervisor_controller: supervisor_controller, process_id: process_id)
end

#remove(supervisor_controller) ⇒ Object

Remove a worker from the process monitor.

This is provided for consistency with MemoryMonitor, but since we monitor the entire process tree via ppid, we don’t need to track individual workers.



48
49
50
51
# File 'lib/async/service/supervisor/process_monitor.rb', line 48

def remove(supervisor_controller)
	process_id = supervisor_controller.process_id
	Console.debug(self, "Worker removed.", supervisor_controller: supervisor_controller, process_id: process_id)
end

#run_onceObject

Run one iteration of the process monitor.



81
82
83
# File 'lib/async/service/supervisor/process_monitor.rb', line 81

def run_once
	self.emit(self.metrics)
end