use crate::types::{Layer3Result, ProcessInfo, ProcessState};
use async_trait::async_trait;
#[async_trait]
pub trait ProcessManager: Send + Sync {
async fn start(
&self,
command: String,
args: Vec<String>,
working_dir: Option<String>,
) -> Layer3Result<u32>;
async fn stop(&self, pid: u32, force: bool) -> Layer3Result<bool>;
async fn get_info(&self, pid: u32) -> Layer3Result<Option<ProcessInfo>>;
async fn get_state(&self, pid: u32) -> Layer3Result<ProcessState>;
async fn is_alive(&self, pid: u32) -> Layer3Result<bool>;
async fn wait(&self, pid: u32) -> Layer3Result<i32>;
async fn list(&self) -> Layer3Result<Vec<ProcessInfo>>;
async fn list_by_state(&self, state: ProcessState) -> Layer3Result<Vec<ProcessInfo>>;
async fn signal(&self, pid: u32, signal: ProcessSignal) -> Layer3Result<bool>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProcessSignal {
Terminate,
Kill,
Stop,
Continue,
Interrupt,
}
#[async_trait]
pub trait OutputCapture: Send + Sync {
async fn get_stdout(&self, pid: u32) -> Layer3Result<String>;
async fn get_stderr(&self, pid: u32) -> Layer3Result<String>;
async fn stream_output(&self, pid: u32) -> Layer3Result<ProcessOutputStream>;
}
#[derive(Debug)]
pub struct ProcessOutputStream {
pub stdout_lines: Vec<String>,
pub stderr_lines: Vec<String>,
pub finished: bool,
}
#[async_trait]
pub trait ProcessMonitor: Send + Sync {
async fn cpu_usage(&self, pid: u32) -> Layer3Result<f32>;
async fn memory_usage(&self, pid: u32) -> Layer3Result<u64>;
async fn runtime(&self, pid: u32) -> Layer3Result<u64>;
async fn set_limits(&self, pid: u32, limits: ProcessLimits) -> Layer3Result<bool>;
}
#[derive(Debug, Clone, Default)]
pub struct ProcessLimits {
pub max_cpu_secs: Option<u64>,
pub max_memory_bytes: Option<u64>,
pub max_runtime_secs: Option<u64>,
pub max_output_bytes: Option<u64>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process_limits_default() {
let limits = ProcessLimits::default();
assert!(limits.max_cpu_secs.is_none());
assert!(limits.max_memory_bytes.is_none());
}
#[test]
fn test_process_signal() {
assert_eq!(ProcessSignal::Terminate, ProcessSignal::Terminate);
}
}