use std::time::Duration;
use taskflow_rs::{TaskDefinition, TaskFlow, framework::TaskFlowConfig};
use tracing_subscriber::fmt::init;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
init();
let config = TaskFlowConfig::with_in_memory();
let taskflow = TaskFlow::new(config).await?;
println!("TaskFlow framework started!");
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 dependent_task = TaskDefinition::new("dependent_task", "shell_command")
.with_payload("command", serde_json::Value::String("echo".to_string()))
.with_payload(
"args",
serde_json::Value::Array(vec![serde_json::Value::String(
"This task depends on the shell task".to_string(),
)]),
)
.with_dependencies(vec![shell_task_id.clone()]);
let dependent_task_id = taskflow.submit_task(dependent_task).await?;
println!("Submitted dependent task: {}", dependent_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(())
}