Skip to main content

systemprompt_traits/
process.rs

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