taskflow-rs 0.1.1

A high-performance, async-first task orchestration framework for Rust
Documentation
use std::time::Duration;
use taskflow_rs::TaskFlow;
use tracing_subscriber::fmt::init;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    init();

    // Method 1: Load from YAML file
    let taskflow = TaskFlow::from_yaml_file("examples/config.yaml").await?;
    println!("TaskFlow framework started with YAML configuration!");

    // Method 2: You can also load from YAML string directly
    /*
    let yaml_config = r#"
scheduler:
  poll_interval_seconds: 2
  max_concurrent_tasks: 30
  enable_dependency_resolution: true
  cleanup_completed_tasks_after_hours: 24

executor:
  worker_id: "custom-worker-001"
  max_concurrent_tasks: 15
  task_timeout_seconds: 300
  heartbeat_interval_seconds: 10

storage_type: InMemory
"#;
    let taskflow = TaskFlow::from_yaml_str(yaml_config).await?;
    "#;
    */

    let task_id = taskflow
        .submit_http_task("fetch_example", "https://httpbin.org/get", Some("GET"))
        .await?;

    println!("Submitted HTTP task: {}", task_id);

    let shell_task_id = taskflow
        .submit_shell_task("list_files", "ls", vec!["-la"])
        .await?;

    println!("Submitted shell task: {}", shell_task_id);

    let taskflow_clone = std::sync::Arc::new(taskflow);
    let taskflow_for_execution = taskflow_clone.clone();

    let execution_handle = tokio::spawn(async move {
        if let Err(e) = taskflow_for_execution.start().await {
            eprintln!("TaskFlow execution failed: {}", e);
        }
    });

    tokio::time::sleep(Duration::from_secs(2)).await;

    loop {
        let metrics = taskflow_clone.get_task_metrics().await?;
        println!(
            "Task metrics: pending={}, running={}, completed={}, failed={}, total={}",
            metrics.pending, metrics.running, metrics.completed, metrics.failed, metrics.total
        );

        if metrics.pending == 0 && metrics.running == 0 {
            break;
        }

        tokio::time::sleep(Duration::from_secs(1)).await;
    }

    println!("All tasks completed!");

    let tasks = taskflow_clone.list_tasks(None).await?;
    for task in tasks {
        println!("Task: {} - Status: {:?}", task.definition.name, task.status);
        if let Some(result) = &task.result {
            if result.success {
                println!("  Output: {:?}", result.output);
            } else {
                println!("  Error: {:?}", result.error);
            }
        }
    }

    taskflow_clone.shutdown().await?;
    execution_handle.abort();

    Ok(())
}