use crate::health::HealthCheckerTrait;
use crate::models::{HealthResult, Service};
use async_trait::async_trait;
use sysinfo::{Pid, System};
use tracing::trace;
pub struct ProcessHealthChecker;
impl ProcessHealthChecker {
pub fn new() -> Self {
Self
}
}
#[async_trait]
impl HealthCheckerTrait for ProcessHealthChecker {
async fn check(&self, service: &Service) -> HealthResult {
if let Some(pid) = service.pid {
let mut sys = System::new();
sys.refresh_processes();
if sys.process(Pid::from_u32(pid)).is_some() {
trace!("Process {} is running", pid);
HealthResult::healthy(0)
} else {
trace!("Process {} is not running", pid);
HealthResult::not_running(
service.command_line.as_ref().map(|cmd| {
format!("Try running: {}", cmd)
})
)
}
} else {
HealthResult::unhealthy(
"No process information available".to_string(),
None,
)
}
}
fn supports(&self, service: &Service) -> bool {
service.pid.is_some()
}
}