TaskEvent

Enum TaskEvent 

Source
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:

  1. Started - Process has been spawned
  2. Output - Output lines from stdout/stderr (ongoing)
  3. Ready - Ready indicator detected (optional, for long-running processes)
  4. Stopped - Process has completed, with exit code and reason
  5. Error - 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.

Fields

§task_name: String

Name of the task that started

§

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

§task_name: String

Name of the task that produced the output

§line: String

The output line (without trailing newline)

§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.

Fields

§task_name: String

Name of the task that became ready

§

Stopped

Process has completed execution

The process has exited and all resources have been cleaned up.

Fields

§task_name: String

Name of the task that stopped

§exit_code: Option<i32>

Exit code from the process (None if terminated)

§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.

Fields

§task_name: String

Name of the task that encountered an error

§error: TaskError

The specific error that occurred

Trait Implementations§

Source§

impl Clone for TaskEvent

Source§

fn clone(&self) -> TaskEvent

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TaskEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for TaskEvent

Source§

fn eq(&self, other: &TaskEvent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for TaskEvent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.