Skip to main content

systemprompt_traits/
process.rs

1//! Process and port management provider trait.
2
3use async_trait::async_trait;
4use std::sync::Arc;
5
6pub type ProcessResult<T> = Result<T, ProcessProviderError>;
7
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum ProcessProviderError {
11    #[error("Process not found: {0}")]
12    NotFound(u32),
13
14    #[error("Operation failed: {0}")]
15    OperationFailed(String),
16
17    #[error("Timeout waiting for port {0}")]
18    PortTimeout(u16),
19
20    #[error("Internal error: {0}")]
21    Internal(String),
22}
23
24impl From<anyhow::Error> for ProcessProviderError {
25    fn from(err: anyhow::Error) -> Self {
26        Self::Internal(err.to_string())
27    }
28}
29
30#[async_trait]
31pub trait ProcessCleanupProvider: Send + Sync {
32    fn process_exists(&self, pid: u32) -> bool;
33
34    fn check_port(&self, port: u16) -> Option<u32>;
35
36    fn kill_process(&self, pid: u32) -> bool;
37
38    async fn terminate_gracefully(&self, pid: u32, grace_period_ms: u64) -> bool;
39
40    async fn wait_for_port_free(
41        &self,
42        port: u16,
43        max_retries: u8,
44        delay_ms: u64,
45    ) -> ProcessResult<()>;
46}
47
48pub type DynProcessCleanupProvider = Arc<dyn ProcessCleanupProvider>;