#[repr(u8)]pub enum TaskState {
Pending = 0,
Initiating = 1,
Running = 2,
Ready = 3,
Finished = 4,
Invalid = 127,
}Expand description
Execution state of a task throughout its lifecycle
TaskState tracks the progression of a task from creation through completion.
States transition in a defined order, enabling event-driven tasks execution.
§State Transitions
Pending → Initiating → Running → [Ready] → Finished
↘
→ FinishedThe Ready state is optional and only occurs for long-running processes with a configured ready indicator.
§Examples
§State Monitoring
use tcrm_task::tasks::{config::TaskConfig, tokio::executor::TaskExecutor, state::TaskState, control::TaskStatusInfo};
use tokio::sync::mpsc;
#[tokio::main]
async fn main() {
#[cfg(windows)]
let config = TaskConfig::new("cmd").args(["/C", "echo", "hello"]);
#[cfg(unix)]
let config = TaskConfig::new("echo").args(["hello"]);
let (tx, _rx) = mpsc::channel(100);
let executor = TaskExecutor::new(config, tx);
// Initially pending
assert_eq!(executor.get_task_state(), TaskState::Pending);
// After calling coordinate_start(), state will progress through:
// Pending → Initiating → Running → Finished
}§Basic State Checking
use tcrm_task::tasks::{
config::TaskConfig,
tokio::executor::TaskExecutor,
state::TaskState,
control::TaskStatusInfo
};
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(windows)]
let config = TaskConfig::new("cmd").args(["/C", "echo", "hello"]);
#[cfg(unix)]
let config = TaskConfig::new("echo").args(["hello"]);
let (tx, _rx) = mpsc::channel(100);
let executor = TaskExecutor::new(config, tx);
// Check initial state
let state = executor.get_task_state();
assert_eq!(state, TaskState::Pending);
println!("Task is in {:?} state", state);
Ok(())
}Variants§
Pending = 0
Task has been created but not yet started
Initial state when TaskSpawner is created. The task configuration
exists but no process has been spawned yet.
Initiating = 1
Task is being initialized and validated
Transitional state during start_direct() when configuration is being
validated and the process is being prepared for spawning.
Running = 2
Process is running and executing
The system process has been spawned and is actively executing. Output events may be emitted during this state.
Ready = 3
Process is running and executing
Only reached by long-running processes that have a ready indicator configured. Indicates the process has completed initialization and is ready for work (e.g., web server listening on port). Useful when orchestrating dependent tasks.
Finished = 4
Task execution has completed
Final state reached when the process exits normally, is terminated, or encounters an error. No further state transitions occur.
Invalid = 127
Invalid state (should not occur)
This state indicates an error in state management. It should not be possible to reach this state during normal operation.
Internal use only.