yaml_config_usage/
yaml_config_usage.rs1use 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 let taskflow = TaskFlow::from_yaml_file("examples/config.yaml").await?;
11 println!("TaskFlow framework started with YAML configuration!");
12
13 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}