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)
	@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 status

Get status for the process monitor.

Signature

returns Hash

Status including process metrics.

Implementation

def status
	{process_monitor: {ppid: @ppid, metrics: self.metrics}}
end

def run

Run the process monitor.

Periodically captures and logs process metrics for the entire process tree.

Signature

returns Async::Task

The task that is running the process monitor.

Implementation

def run
	Async do
		Loop.run(interval: @interval) do
			metrics = self.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
	end
end