yaml_config_usage/
yaml_config_usage.rs

1use std::time::Duration;
2use taskflow_rs::TaskFlow;
3use tracing_subscriber::fmt::init;
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    init();
8
9    // Method 1: Load from YAML file
10    let taskflow = TaskFlow::from_yaml_file("examples/config.yaml").await?;
11    println!("TaskFlow framework started with YAML configuration!");
12
13    // Method 2: You can also load from YAML string directly
14    /*
15    let yaml_config = r#"
16scheduler:
17  poll_interval_seconds: 2
18  max_concurrent_tasks: 30
19  enable_dependency_resolution: true
20  cleanup_completed_tasks_after_hours: 24
21
22executor:
23  worker_id: "custom-worker-001"
24  max_concurrent_tasks: 15
25  task_timeout_seconds: 300
26  heartbeat_interval_seconds: 10
27
28storage_type: InMemory
29"#;
30    let taskflow = TaskFlow::from_yaml_str(yaml_config).await?;
31    "#;
32    */
33
34    let task_id = taskflow
35        .submit_http_task("fetch_example", "https://httpbin.org/get", Some("GET"))
36        .await?;
37
38    println!("Submitted HTTP task: {}", task_id);
39
40    let shell_task_id = taskflow
41        .submit_shell_task("list_files", "ls", vec!["-la"])
42        .await?;
43
44    println!("Submitted shell task: {}", shell_task_id);
45
46    let taskflow_clone = std::sync::Arc::new(taskflow);
47    let taskflow_for_execution = taskflow_clone.clone();
48
49    let execution_handle = tokio::spawn(async move {
50        if let Err(e) = taskflow_for_execution.start().await {
51            eprintln!("TaskFlow execution failed: {}", e);
52        }
53    });
54
55    tokio::time::sleep(Duration::from_secs(2)).await;
56
57    loop {
58        let metrics = taskflow_clone.get_task_metrics().await?;
59        println!(
60            "Task metrics: pending={}, running={}, completed={}, failed={}, total={}",
61            metrics.pending, metrics.running, metrics.completed, metrics.failed, metrics.total
62        );
63
64        if metrics.pending == 0 && metrics.running == 0 {
65            break;
66        }
67
68        tokio::time::sleep(Duration::from_secs(1)).await;
69    }
70
71    println!("All tasks completed!");
72
73    let tasks = taskflow_clone.list_tasks(None).await?;
74    for task in tasks {
75        println!("Task: {} - Status: {:?}", task.definition.name, task.status);
76        if let Some(result) = &task.result {
77            if result.success {
78                println!("  Output: {:?}", result.output);
79            } else {
80                println!("  Error: {:?}", result.error);
81            }
82        }
83    }
84
85    taskflow_clone.shutdown().await?;
86    execution_handle.abort();
87
88    Ok(())
89}