unistore_process/
lib.rs

1//! # UniStore Process
2//!
3//! 进程管理能力模块,提供子进程创建和管理功能。
4//!
5//! ## 功能特性
6//!
7//! - 异步进程创建
8//! - 标准输入/输出/错误流管理
9//! - 进程生命周期管理
10//! - 优雅终止与强制终止
11//! - 环境变量配置
12//! - 工作目录设置
13//!
14//! ## 使用示例
15//!
16//! ```ignore
17//! use unistore_process::{Command, ProcessOutput};
18//!
19//! // 简单命令执行
20//! let output = Command::new("echo")
21//!     .arg("Hello, World!")
22//!     .output()
23//!     .await?;
24//!
25//! println!("stdout: {}", output.stdout_string());
26//!
27//! // 后台进程
28//! let mut child = Command::new("my-server")
29//!     .spawn()
30//!     .await?;
31//!
32//! // 等待完成或超时
33//! let status = child.wait_timeout(Duration::from_secs(30)).await?;
34//! ```
35
36mod command;
37mod error;
38mod output;
39mod child;
40
41pub use command::{Command, CommandBuilder, shell, exec, which};
42pub use error::ProcessError;
43pub use output::ProcessOutput;
44pub use child::{Child, ExitStatus};
45
46use async_trait::async_trait;
47use unistore_core::{Capability, CapabilityError, CapabilityInfo};
48
49/// 进程管理能力
50pub struct ProcessCapability {
51    started: std::sync::atomic::AtomicBool,
52}
53
54impl ProcessCapability {
55    /// 创建新实例
56    pub fn new() -> Self {
57        Self {
58            started: std::sync::atomic::AtomicBool::new(false),
59        }
60    }
61}
62
63impl Default for ProcessCapability {
64    fn default() -> Self {
65        Self::new()
66    }
67}
68
69#[async_trait]
70impl Capability for ProcessCapability {
71    fn info(&self) -> CapabilityInfo {
72        CapabilityInfo::new("process", "0.1.0")
73            .with_description("Process management capability")
74    }
75
76    async fn start(&mut self) -> Result<(), CapabilityError> {
77        self.started
78            .store(true, std::sync::atomic::Ordering::SeqCst);
79        tracing::info!("Process capability started");
80        Ok(())
81    }
82
83    async fn stop(&mut self) -> Result<(), CapabilityError> {
84        self.started
85            .store(false, std::sync::atomic::Ordering::SeqCst);
86        tracing::info!("Process capability stopped");
87        Ok(())
88    }
89
90    async fn health_check(&self) -> Result<(), CapabilityError> {
91        if self.started.load(std::sync::atomic::Ordering::SeqCst) {
92            Ok(())
93        } else {
94            Err(CapabilityError::unhealthy("process", "capability not started"))
95        }
96    }
97}