pub enum TaskEvent {
Started {
task_name: String,
},
Output {
task_name: String,
line: String,
src: StreamSource,
},
Ready {
task_name: String,
},
Stopped {
task_name: String,
exit_code: Option<i32>,
reason: TaskEventStopReason,
},
Error {
task_name: String,
error: TaskError,
},
}
Expand description
Events emitted during task execution lifecycle
TaskEvent
represents all events that occur during task execution,
from process start to completion. These events enable real-time monitoring
and event-driven programming patterns.
§Event Flow
A typical task execution emits events in this order:
Started
- Process has been spawnedOutput
- Output lines from stdout/stderr (ongoing)Ready
- Ready indicator detected (optional, for long-running processes)Stopped
- Process has completed, with exit code and reasonError
- Error related to task execution
§Examples
§Basic Event Processing
use tcrm_task::tasks::{config::TaskConfig, async_tokio::spawner::TaskSpawner, event::TaskEvent};
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = TaskConfig::new("cmd").args(["/C", "echo", "hello", "world"]);
let mut spawner = TaskSpawner::new("demo".to_string(), config);
let (tx, mut rx) = mpsc::channel(100);
spawner.start_direct(tx).await?;
while let Some(event) = rx.recv().await {
match event {
TaskEvent::Started { task_name } => {
println!("Task '{}' started", task_name);
}
TaskEvent::Output { task_name, line, src } => {
println!("Task '{}' output: {}", task_name, line);
}
TaskEvent::Stopped { task_name, exit_code, reason } => {
println!("Task '{}' stopped with code {:?}", task_name, exit_code);
break;
}
TaskEvent::Error { task_name, error } => {
eprintln!("Task '{}' error: {}", task_name, error);
break;
}
_ => {}
}
}
Ok(())
}
§Server Ready Detection
use tcrm_task::tasks::{
config::{TaskConfig, StreamSource},
async_tokio::spawner::TaskSpawner,
event::TaskEvent
};
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = TaskConfig::new("cmd")
.args(["/C", "echo", "Server listening"])
.ready_indicator("Server listening")
.ready_indicator_source(StreamSource::Stdout);
let mut spawner = TaskSpawner::new("server".to_string(), config);
let (tx, mut rx) = mpsc::channel(100);
spawner.start_direct(tx).await?;
while let Some(event) = rx.recv().await {
match event {
TaskEvent::Ready { task_name } => {
println!("Server '{}' is ready for requests!", task_name);
// Server is now ready - can start sending requests
break;
}
TaskEvent::Output { line, .. } => {
println!("Server log: {}", line);
}
_ => {}
}
}
Ok(())
}
Variants§
Started
Process has been successfully spawned and is running
This is the first event emitted after successful process creation. The process is now running and other events will follow.
Output
Output line received from the process
Emitted for each line of output from stdout or stderr. Lines are buffered and emitted when complete (on newline).
Fields
src: StreamSource
Source stream (stdout or stderr)
Ready
Process has signaled it’s ready to accept requests
Only emitted for long-running processes that have a ready indicator configured. Indicates the process has completed initialization and is ready for work.
Stopped
Process has completed execution
The process has exited and all resources have been cleaned up.
Fields
reason: TaskEventStopReason
Reason the process stopped
Error
An error occurred before task execution
Emitted when errors occur during configuration validation, process spawning, and will not emit any further events.