tcrm-task 0.4.2

Task execution unit for TCRM project
Documentation
//! Example: Print tracing messages
use tcrm_task::tasks::{
    config::TaskConfig,
    event::{TaskEvent, TaskEventEnvelope},
    tokio::executor::TaskExecutor,
};
use tokio::sync::mpsc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .init();

    #[cfg(windows)]
    let config = TaskConfig::new("cmd")
        .args(["/C", "echo", "Hello!"])
        .timeout_ms(5000);
    #[cfg(unix)]
    let config = TaskConfig::new("echo").args(["Hello!"]).timeout_ms(5000);

    config.validate()?;
    let (event_tx, mut event_rx) = mpsc::channel::<TaskEventEnvelope>(100);
    let mut executor = TaskExecutor::new(config, event_tx);
    executor.coordinate_start().await?;

    while let Some(envelope) = event_rx.recv().await {
        match envelope.event {
            TaskEvent::Output { line, .. } => println!("Output: {}", line),
            TaskEvent::Stopped {
                exit_code,
                reason,
                finished_at,
                ..
            } => {
                println!(
                    "Stopped: {:?}, reason: {:?} at {:?}",
                    exit_code, reason, finished_at
                );
                break;
            }
            TaskEvent::Error { error, .. } => eprintln!("Error: {}", error),
            _ => {}
        }
    }
    Ok(())
}