Skip to main content

syspulse_core/process/
mod.rs

1use async_trait::async_trait;
2use std::path::PathBuf;
3
4use crate::daemon::DaemonSpec;
5use crate::error::Result;
6
7#[derive(Debug)]
8pub struct ProcessInfo {
9    pub pid: u32,
10    pub alive: bool,
11}
12
13#[derive(Debug, Default)]
14pub struct ResourceUsage {
15    pub memory_bytes: u64,
16    pub cpu_percent: f64,
17}
18
19#[async_trait]
20pub trait ProcessDriver: Send + Sync {
21    async fn spawn(
22        &self,
23        spec: &DaemonSpec,
24        stdout_path: &PathBuf,
25        stderr_path: &PathBuf,
26    ) -> Result<ProcessInfo>;
27
28    async fn stop(&self, pid: u32, timeout_secs: u64) -> Result<()>;
29    async fn kill(&self, pid: u32) -> Result<()>;
30    async fn is_alive(&self, pid: u32) -> bool;
31    async fn wait(&self, pid: u32) -> Result<Option<i32>>;
32    async fn resource_usage(&self, pid: u32) -> Result<ResourceUsage>;
33}
34
35#[cfg(unix)]
36mod unix;
37#[cfg(windows)]
38mod windows;
39
40pub fn create_driver() -> Box<dyn ProcessDriver> {
41    #[cfg(unix)]
42    {
43        Box::new(unix::UnixProcessDriver::new())
44    }
45    #[cfg(windows)]
46    {
47        Box::new(windows::WindowsProcessDriver::new())
48    }
49}