tcrm_task/tasks/state.rs
1/// Execution state of a task throughout its lifecycle
2///
3/// `TaskState` tracks the progression of a task from creation through completion.
4/// States transition in a defined order, enabling event-driven tasks execution.
5///
6/// # State Transitions
7///
8/// ```text
9/// Pending → Initiating → Running → [Ready] → Finished
10/// ↘
11/// → Finished
12/// ```
13///
14/// The Ready state is optional and only occurs for long-running processes
15/// with a configured ready indicator.
16///
17/// # Examples
18///
19/// ## State Monitoring
20/// ```rust
21/// use tcrm_task::tasks::{config::TaskConfig, async_tokio::spawner::TaskSpawner, state::TaskState};
22///
23/// #[tokio::main]
24/// async fn main() {
25/// let config = TaskConfig::new("cmd").args(["/C", "echo", "hello"]);
26/// let spawner = TaskSpawner::new("test".to_string(), config);
27///
28/// // Initially pending
29/// assert_eq!(spawner.get_state().await, TaskState::Pending);
30///
31/// // After calling start_direct(), state will progress through:
32/// // Pending → Initiating → Running → Finished
33/// }
34/// ```
35///
36/// ## Basic State Checking
37/// ```rust
38/// use tcrm_task::tasks::{
39/// config::TaskConfig,
40/// async_tokio::spawner::TaskSpawner,
41/// state::TaskState
42/// };
43///
44/// #[tokio::main]
45/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
46/// let config = TaskConfig::new("cmd").args(["/C", "echo", "hello"]);
47/// let spawner = TaskSpawner::new("demo".to_string(), config);
48///
49/// // Check initial state
50/// let state = spawner.get_state().await;
51/// assert_eq!(state, TaskState::Pending);
52/// println!("Task is in {:?} state", state);
53///
54/// Ok(())
55/// }
56/// ```
57#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
58#[derive(Debug, Clone, PartialEq)]
59pub enum TaskState {
60 /// Task has been created but not yet started
61 ///
62 /// Initial state when `TaskSpawner` is created. The task configuration
63 /// exists but no process has been spawned yet.
64 Pending,
65
66 /// Task is being initialized and validated
67 ///
68 /// Transitional state during `start_direct()` when configuration is being
69 /// validated and the process is being prepared for spawning.
70 Initiating,
71
72 /// Process is running and executing
73 ///
74 /// The system process has been spawned and is actively executing.
75 /// Output events may be emitted during this state.
76 Running,
77
78 /// Process is running and executing
79 ///
80 /// Only reached by long-running processes that have a ready indicator
81 /// configured. Indicates the process has completed initialization
82 /// and is ready for work (e.g., web server listening on port).
83 /// Useful when orchestrating dependent tasks.
84 Ready,
85
86 /// Task execution has completed
87 ///
88 /// Final state reached when the process exits normally, is terminated,
89 /// or encounters an error. No further state transitions occur.
90 Finished,
91}