tcrm_task/tasks/process/
control.rs

1#[cfg(feature = "process-control")]
2use crate::tasks::error::TaskError;
3
4#[cfg(feature = "process-control")]
5pub trait ProcessControl {
6    /// Requests the process to stop execution.
7    ///
8    /// # Returns
9    ///
10    /// * `Ok(())` if the stop signal was sent successfully
11    /// * `Err(TaskError)` if stopping fails
12    #[cfg(feature = "tokio")]
13    fn stop_process(&mut self) -> impl futures::Future<Output = Result<(), TaskError>> {
14        self.perform_process_action(ProcessControlAction::Stop)
15    }
16
17    /// Requests the process to pause execution.
18    ///
19    /// # Returns
20    ///
21    /// * `Ok(())` if the pause signal was sent successfully
22    /// * `Err(TaskError)` if pausing fails
23    fn pause_process(&mut self) -> impl futures::Future<Output = Result<(), TaskError>> {
24        self.perform_process_action(ProcessControlAction::Pause)
25    }
26
27    /// Requests the process to resume execution if it is paused.
28    ///
29    /// # Returns
30    ///
31    /// * `Ok(())` if the resume signal was sent successfully
32    /// * `Err(TaskError)` if resuming fails
33    fn resume_process(&mut self) -> impl futures::Future<Output = Result<(), TaskError>> {
34        self.perform_process_action(ProcessControlAction::Resume)
35    }
36
37    /// Performs a control action on the process or process group.
38    ///
39    /// # Arguments
40    ///
41    /// * `action` - The control action to perform (stop, pause, resume)
42    ///
43    /// # Returns
44    ///
45    /// * `Ok(())` if the action was performed successfully
46    /// * `Err(TaskError)` if the action fails
47    ///
48    /// # Errors
49    ///
50    /// Returns [`TaskError::Control`] if the process is not in a controllable state
51    /// or if the requested action is not supported
52    fn perform_process_action(
53        &mut self,
54        action: ProcessControlAction,
55    ) -> impl futures::Future<Output = Result<(), TaskError>>;
56}
57
58/// Actions that can be performed on a process.
59///
60/// Defines the possible control actions that can be
61/// applied to a running process, such as stopping, pausing, or resuming.
62#[cfg(feature = "process-control")]
63#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum ProcessControlAction {
66    /// Stop process execution.
67    Stop,
68    /// Pause process execution.
69    Pause,
70    /// Resume paused process execution.
71    Resume,
72}