Skip to main content

sh_layer3/
process_manager.rs

1//! # Process Manager
2//!
3//! 进程管理器:管理后台进程的生命周期。
4
5use crate::types::{Layer3Result, ProcessInfo, ProcessState};
6use async_trait::async_trait;
7
8/// 进程管理器 trait
9///
10/// 定义进程管理核心接口。
11#[async_trait]
12pub trait ProcessManager: Send + Sync {
13    /// 启动新进程
14    ///
15    /// # Arguments
16    /// * `command` - 命令行
17    /// * `args` - 参数
18    /// * `working_dir` - 工作目录
19    ///
20    /// # Returns
21    /// * `u32` - 进程 PID
22    async fn start(
23        &self,
24        command: String,
25        args: Vec<String>,
26        working_dir: Option<String>,
27    ) -> Layer3Result<u32>;
28
29    /// 停止进程
30    ///
31    /// # Arguments
32    /// * `pid` - 进程 PID
33    /// * `force` - 是否强制终止
34    async fn stop(&self, pid: u32, force: bool) -> Layer3Result<bool>;
35
36    /// 获取进程信息
37    async fn get_info(&self, pid: u32) -> Layer3Result<Option<ProcessInfo>>;
38
39    /// 获取进程状态
40    async fn get_state(&self, pid: u32) -> Layer3Result<ProcessState>;
41
42    /// 检查进程是否存活
43    async fn is_alive(&self, pid: u32) -> Layer3Result<bool>;
44
45    /// 等待进程结束
46    async fn wait(&self, pid: u32) -> Layer3Result<i32>;
47
48    /// 列出所有管理的进程
49    async fn list(&self) -> Layer3Result<Vec<ProcessInfo>>;
50
51    /// 列出指定状态的进程
52    async fn list_by_state(&self, state: ProcessState) -> Layer3Result<Vec<ProcessInfo>>;
53
54    /// 发送信号到进程
55    async fn signal(&self, pid: u32, signal: ProcessSignal) -> Layer3Result<bool>;
56}
57
58/// 进程信号
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum ProcessSignal {
61    /// 终止信号 (SIGTERM)
62    Terminate,
63    /// 强制终止 (SIGKILL)
64    Kill,
65    /// 暂停 (SIGSTOP)
66    Stop,
67    /// 继续 (SIGCONT)
68    Continue,
69    /// 中断 (SIGINT)
70    Interrupt,
71}
72
73/// 进程输出捕获 trait
74#[async_trait]
75pub trait OutputCapture: Send + Sync {
76    /// 获取进程 stdout
77    async fn get_stdout(&self, pid: u32) -> Layer3Result<String>;
78
79    /// 获取进程 stderr
80    async fn get_stderr(&self, pid: u32) -> Layer3Result<String>;
81
82    /// 流式读取输出
83    async fn stream_output(&self, pid: u32) -> Layer3Result<ProcessOutputStream>;
84}
85
86/// 进程输出流
87#[derive(Debug)]
88pub struct ProcessOutputStream {
89    /// stdout 行
90    pub stdout_lines: Vec<String>,
91    /// stderr 行
92    pub stderr_lines: Vec<String>,
93    /// 是否结束
94    pub finished: bool,
95}
96
97/// 进程监控 trait
98#[async_trait]
99pub trait ProcessMonitor: Send + Sync {
100    /// 获取 CPU 使用率
101    async fn cpu_usage(&self, pid: u32) -> Layer3Result<f32>;
102
103    /// 获取内存使用
104    async fn memory_usage(&self, pid: u32) -> Layer3Result<u64>;
105
106    /// 获取运行时间
107    async fn runtime(&self, pid: u32) -> Layer3Result<u64>;
108
109    /// 设置资源限制
110    async fn set_limits(&self, pid: u32, limits: ProcessLimits) -> Layer3Result<bool>;
111}
112
113/// 进程资源限制
114#[derive(Debug, Clone, Default)]
115pub struct ProcessLimits {
116    /// 最大 CPU 时间(秒)
117    pub max_cpu_secs: Option<u64>,
118    /// 最大内存(字节)
119    pub max_memory_bytes: Option<u64>,
120    /// 最大运行时间(秒)
121    pub max_runtime_secs: Option<u64>,
122    /// 最大输出大小(字节)
123    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}