tcrm_monitor/monitor/
event.rs

1//! Event system for task monitoring.
2//!
3//! This module provides event types that allow monitoring the progress and status
4//! of task execution in real-time. Events are emitted during various phases of
5//! task monitoring, from initial setup to completion.
6
7use tcrm_task::tasks::event::TaskEvent;
8
9use crate::monitor::error::TaskMonitorError;
10
11/// Events emitted by `TaskMonitor` during execution.
12///
13/// These events provide real-time information about task execution progress,
14/// control message processing, and error conditions. They can be used to build
15/// user interfaces, logging systems, or progress monitoring.
16///
17/// # Event Categories
18///
19/// - **Task Events**: Forwarded from individual tasks (started, output, stopped, etc.)
20/// - **Execution Events**: Monitor-level events (started, completed)  
21/// - **Control Events**: Events related to runtime control (stop, stdin, terminate)
22/// - **Error Events**: Error conditions during monitoring
23///
24/// # Examples
25///
26/// ## Basic Event Monitoring
27///
28/// ```rust
29/// use tokio::sync::mpsc;
30/// use tcrm_monitor::monitor::event::TaskMonitorEvent;
31///
32/// # #[tokio::main]
33/// # async fn main() {
34/// let (event_tx, mut event_rx) = mpsc::channel(100);
35///
36/// // Spawn a task to handle events
37/// tokio::spawn(async move {
38///     while let Some(event) = event_rx.recv().await {
39///         match event {
40///             TaskMonitorEvent::Started { total_tasks } => {
41///                 println!("Starting execution of {} tasks", total_tasks);
42///             }
43///             TaskMonitorEvent::Task(task_event) => {
44///                 println!("Task event: {:?}", task_event);
45///             }
46///             TaskMonitorEvent::Completed { completed_tasks, failed_tasks } => {
47///                 println!("Execution complete: {} completed, {} failed",
48///                          completed_tasks, failed_tasks);
49///                 break;
50///             }
51///             _ => {}
52///         }
53///     }
54/// });
55/// # }
56/// ```
57
58#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
59#[derive(Debug, Clone)]
60pub enum TaskMonitorEvent {
61    /// A task event from the underlying task system.
62    ///
63    /// This forwards events from individual tasks, including:
64    /// - Task started
65    /// - Task output (stdout/stderr)  
66    /// - Task ready state changes
67    /// - Task stopped/completed
68    /// - Task errors
69    ///
70    /// # Examples
71    ///
72    /// ```rust
73    /// use tcrm_monitor::monitor::event::TaskMonitorEvent;
74    /// use tcrm_task::tasks::event::TaskEvent;
75    ///
76    /// let event = TaskMonitorEvent::Task(TaskEvent::Started {
77    ///     task_name: "build".to_string()
78    /// });
79    /// ```
80    Task(TaskEvent),
81
82    /// Monitor execution started.
83    ///
84    /// Emitted when task execution begins, providing the total number of tasks to execute.
85    ///
86    /// # Fields
87    ///
88    /// * `total_tasks` - Total number of tasks in the execution plan
89    ///
90    /// # Examples
91    ///
92    /// ```rust
93    /// use tcrm_monitor::monitor::event::TaskMonitorEvent;
94    ///
95    /// let event = TaskMonitorEvent::Started { total_tasks: 5 };
96    /// ```
97    Started {
98        /// Total number of tasks to execute
99        total_tasks: usize,
100    },
101
102    /// Monitor execution completed.
103    ///
104    /// Emitted when all tasks have finished (either successfully or with errors).
105    ///
106    /// # Fields
107    ///
108    /// * `completed_tasks` - Number of tasks that completed successfully
109    /// * `failed_tasks` - Number of tasks that failed or were terminated
110    ///
111    /// # Examples
112    ///
113    /// ```rust
114    /// use tcrm_monitor::monitor::event::TaskMonitorEvent;
115    ///
116    /// let event = TaskMonitorEvent::Completed {
117    ///     completed_tasks: 3,
118    ///     failed_tasks: 1
119    /// };
120    /// ```
121    Completed {
122        /// Number of tasks completed successfully
123        completed_tasks: usize,
124        /// Number of tasks that failed
125        failed_tasks: usize,
126    },
127
128    Control(TaskMonitorControlEvent),
129
130    Error(TaskMonitorError),
131}
132
133#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
134#[derive(Debug, Clone)]
135pub enum TaskMonitorControlEvent {
136    ControlReceived { control: TaskMonitorControlCommand },
137    ControlProcessed { control: TaskMonitorControlCommand },
138}
139
140/// Control message for `TaskMonitor` execution.
141///
142/// These control messages allow runtime interaction with task execution,
143/// providing the ability to stop tasks, send input, or terminate specific tasks.
144///
145/// # Examples
146///
147/// ## Terminating All Tasks
148///
149/// ```rust
150/// use tcrm_monitor::monitor::event::TaskMonitorControlCommand;
151///
152/// let control = TaskMonitorControlCommand::TerminateAllTasks;
153/// ```
154///
155/// ## Sending Stdin to a Task
156///
157/// ```rust
158/// use tcrm_monitor::monitor::event::TaskMonitorControlCommand;
159///
160/// let control = TaskMonitorControlCommand::SendStdin {
161///     task_name: "interactive_task".to_string(),
162///     input: "y\n".to_string()
163/// };
164/// ```
165///
166/// ## Terminating a Specific Task
167///
168/// ```rust
169/// use tcrm_monitor::monitor::event::TaskMonitorControlCommand;
170///
171/// let control = TaskMonitorControlCommand::TerminateTask {
172///     task_name: "runaway_task".to_string()
173/// };
174/// ```
175#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
176#[derive(Debug, Clone)]
177pub enum TaskMonitorControlCommand {
178    /// Stop all tasks gracefully and terminate execution.
179    ///
180    /// This will attempt to stop all running tasks in an orderly fashion,
181    /// waiting for them to complete their current operations before terminating.
182    TerminateAllTasks,
183
184    /// Terminate a specific task by name.
185    ///
186    /// This forcefully terminates the specified task without waiting for
187    /// it to complete naturally.
188    ///
189    /// # Fields
190    ///
191    /// * `task_name` - Name of the task to terminate
192    TerminateTask {
193        /// Name of the task to terminate
194        task_name: String,
195    },
196
197    /// Send stdin input to a specific task.
198    ///
199    /// Only works if the target task was configured with `enable_stdin(true)`.
200    /// The input will be delivered to the task's stdin stream.
201    ///
202    /// # Fields
203    ///
204    /// * `task_name` - Name of the target task
205    /// * `input` - String input to send to the task's stdin
206    ///
207    /// # Examples
208    ///
209    /// ```rust
210    /// use tcrm_monitor::monitor::event::TaskMonitorControlCommand;
211    ///
212    /// // Send "yes" followed by newline to a task
213    /// let control = TaskMonitorControlCommand::SendStdin {
214    ///     task_name: "confirmation_task".to_string(),
215    ///     input: "yes\n".to_string()
216    /// };
217    /// ```
218    SendStdin {
219        /// Name of the target task
220        task_name: String,
221        /// Input string to send to stdin
222        input: String,
223    },
224}