pub enum TaskEvent {
Started {
process_id: u32,
created_at: SystemTime,
running_at: SystemTime,
},
Output {
line: String,
src: StreamSource,
},
Ready,
Stopped {
exit_code: Option<i32>,
reason: TaskStopReason,
finished_at: SystemTime,
signal: Option<Signal>,
},
Error {
error: TaskError,
},
ProcessControl {
action: ProcessControlAction,
},
}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 reason- Exit code is
Some(code)for natural completion - Exit code is
Nonefor terminated processes (timeout, manual termination)
- Exit code is
Error- Error related to task execution
§Examples
§Basic Event Processing
use tcrm_task::tasks::{config::TaskConfig, tokio::executor::TaskExecutor, event::TaskEvent};
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", "world"]);
#[cfg(unix)]
let config = TaskConfig::new("echo").args(["hello", "world"]);
let (tx, mut rx) = mpsc::channel(100);
let mut executor = TaskExecutor::new(config, tx);
executor.coordinate_start().await?;
while let Some(envelope) = rx.recv().await {
match envelope.event {
TaskEvent::Started { process_id, .. } => {
println!("Process started with ID: {}", process_id);
}
TaskEvent::Output { line, .. } => {
println!("Output: {}", line);
}
TaskEvent::Stopped { exit_code, .. } => {
match exit_code {
Some(code) => println!("Process completed with code {}", code),
None => println!("Process was terminated"),
}
break;
}
TaskEvent::Error { error } => {
eprintln!("Error: {}", error);
break;
}
_ => {}
}
}
Ok(())
}§Server Ready Detection
use tcrm_task::tasks::{
config::{TaskConfig, StreamSource},
tokio::executor::TaskExecutor,
event::TaskEvent
};
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", "Server listening"])
.ready_indicator("Server listening")
.ready_indicator_source(StreamSource::Stdout);
#[cfg(unix)]
let config = TaskConfig::new("echo")
.args(["Server listening"])
.ready_indicator("Server listening")
.ready_indicator_source(StreamSource::Stdout);
let (tx, mut rx) = mpsc::channel(100);
let mut executor = TaskExecutor::new(config, tx);
executor.coordinate_start().await?;
while let Some(envelope) = rx.recv().await {
match envelope.event {
TaskEvent::Ready => {
println!("Server is ready for requests!");
// 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 spawning. The process is now running and other events will follow.
§Fields
process_id- Operating system process ID of the spawned processcreated_at- Timestamp when the process was createdrunning_at- Timestamp when the process started running
Fields
created_at: SystemTimeTimestamp when the process was created
running_at: SystemTimeTimestamp when the process started running
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
line- The output line (without trailing newline)src- Source stream (stdout or stderr)
Fields
src: StreamSourceSource stream (stdout or stderr)
Ready
task has signaled it’s ready to work.
Only emitted for long-running processes that have a ready indicator configured. Indicates the task has completed initialization and is ready for work (e.g., server is listening).
Stopped
Task has completed execution.
The task has exited and all resources have been cleaned up.
§Fields
exit_code- Exit code from the processSome(code)- Process completed naturally with exit codeNone- Process was terminated (timeout, user request, etc.)
reason- Reason the process stoppedfinished_at- Timestamp when the process finishedsignal(Unix only) - Termination signal if the process was killed by a signal
Fields
exit_code: Option<i32>Exit code from the process
Some(code)- Process completed naturally with exit codeNone- Process was terminated (timeout, user request, etc.)
Note: Terminated processes do not provide exit codes to avoid race conditions between termination and natural completion.
reason: TaskStopReasonReason the process stopped
finished_at: SystemTimeTimestamp when the process finished
Error
An error occurred during task execution.
Emitted when errors occur during configuration validation, process spawning, or input/output operations.
§Fields
error- The specific error that occurred
ProcessControl
Process control action event.
Emitted when a process control action (pause, resume, stop) is performed on the running process.
§Fields
action- The process control action that was performed (pause, resume, stop)
Fields
action: ProcessControlActionThe process control action that was performed (pause, resume, stop)