Skip to main content

systemprompt_traits/
process.rs

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