Async::Service::SupervisorSourceAsyncServiceSupervisorProcessMonitor

class ProcessMonitor

Monitors process metrics and logs them periodically.

Uses the process-metrics gem to capture CPU and memory metrics for a process tree. Unlike class Async::Service::Supervisor::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.

Definitions

def initialize(interval: 60, ppid: nil)

Create a new process monitor.

Signature

parameter interval Integer

The interval in seconds at which to log process metrics.

parameter ppid Integer

The parent process ID to monitor. If nil, uses the current process to capture its children.

Implementation

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

attr :ppid

Signature

attribute Integer

The parent process ID being monitored.

def register(supervisor_controller)

Register a worker with the process monitor.

This is provided for consistency with class Async::Service::Supervisor::MemoryMonitor, but since we monitor the entire process tree via ppid, we don't need to track individual workers.

Signature

parameter supervisor_controller SupervisorController

The supervisor controller for the worker.

Implementation

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

def remove(supervisor_controller)

Remove a worker from the process monitor.

This is provided for consistency with class Async::Service::Supervisor::MemoryMonitor, but since we monitor the entire process tree via ppid, we don't need to track individual workers.

Signature

parameter supervisor_controller SupervisorController

The supervisor controller for the worker.

Implementation

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

def metrics

Capture current process metrics for the entire process tree.

Signature

returns Hash

A hash mapping process IDs to their metrics.

Implementation

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

def self.monitor_type

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

Implementation

def self.monitor_type
	:process_monitor
end

def as_json

Serialize process metrics for JSON.

Implementation

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

def emit(metrics)

Emit the process metrics.

Signature

parameter metrics Hash

The process metrics to emit.

Implementation

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

def run_once

Run one iteration of the process monitor.

Implementation

def run_once
	self.emit(self.metrics)
end