sh_layer3/
process_manager.rs1use crate::types::{Layer3Result, ProcessInfo, ProcessState};
6use async_trait::async_trait;
7
8#[async_trait]
12pub trait ProcessManager: Send + Sync {
13 async fn start(
23 &self,
24 command: String,
25 args: Vec<String>,
26 working_dir: Option<String>,
27 ) -> Layer3Result<u32>;
28
29 async fn stop(&self, pid: u32, force: bool) -> Layer3Result<bool>;
35
36 async fn get_info(&self, pid: u32) -> Layer3Result<Option<ProcessInfo>>;
38
39 async fn get_state(&self, pid: u32) -> Layer3Result<ProcessState>;
41
42 async fn is_alive(&self, pid: u32) -> Layer3Result<bool>;
44
45 async fn wait(&self, pid: u32) -> Layer3Result<i32>;
47
48 async fn list(&self) -> Layer3Result<Vec<ProcessInfo>>;
50
51 async fn list_by_state(&self, state: ProcessState) -> Layer3Result<Vec<ProcessInfo>>;
53
54 async fn signal(&self, pid: u32, signal: ProcessSignal) -> Layer3Result<bool>;
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum ProcessSignal {
61 Terminate,
63 Kill,
65 Stop,
67 Continue,
69 Interrupt,
71}
72
73#[async_trait]
75pub trait OutputCapture: Send + Sync {
76 async fn get_stdout(&self, pid: u32) -> Layer3Result<String>;
78
79 async fn get_stderr(&self, pid: u32) -> Layer3Result<String>;
81
82 async fn stream_output(&self, pid: u32) -> Layer3Result<ProcessOutputStream>;
84}
85
86#[derive(Debug)]
88pub struct ProcessOutputStream {
89 pub stdout_lines: Vec<String>,
91 pub stderr_lines: Vec<String>,
93 pub finished: bool,
95}
96
97#[async_trait]
99pub trait ProcessMonitor: Send + Sync {
100 async fn cpu_usage(&self, pid: u32) -> Layer3Result<f32>;
102
103 async fn memory_usage(&self, pid: u32) -> Layer3Result<u64>;
105
106 async fn runtime(&self, pid: u32) -> Layer3Result<u64>;
108
109 async fn set_limits(&self, pid: u32, limits: ProcessLimits) -> Layer3Result<bool>;
111}
112
113#[derive(Debug, Clone, Default)]
115pub struct ProcessLimits {
116 pub max_cpu_secs: Option<u64>,
118 pub max_memory_bytes: Option<u64>,
120 pub max_runtime_secs: Option<u64>,
122 pub max_output_bytes: Option<u64>,
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 #[test]
131 fn test_process_limits_default() {
132 let limits = ProcessLimits::default();
133 assert!(limits.max_cpu_secs.is_none());
134 assert!(limits.max_memory_bytes.is_none());
135 }
136
137 #[test]
138 fn test_process_signal() {
139 assert_eq!(ProcessSignal::Terminate, ProcessSignal::Terminate);
140 }
141}