Hierarchical Workflows
Hierarchical Workflows (Multi-Level)
For complex business processes, you can create multi-level orchestration hierarchies where a parent spawns children that themselves spawn grandchildren. Each level handles its own scope of responsibility — the top-level orchestrator coordinates high-level phases, mid-level workflows manage domain-specific logic, and leaf-level workflows perform individual operations. Keep hierarchies shallow (2-3 levels) for easier debugging and observability.
level 1 — orchestrator
Order Pipeline
level 2 — domain workflows
Payment
Validate
Charge
Receipt
Fulfillment
Pick
Pack
Label
Notification
Email
SMS
Push
level 3 — leaf operations
1use orcher::prelude::*;2use anyhow::Result;34// Level 3 - Leaf operation5#[workflow(name = "send_notification", version = "1.0.0")]6pub async fn send_notification(7 ctx: WorkflowContext,8 notification: NotificationData,9) -> Result<NotificationResult> {10 ctx.execute_task(send_email, notification.email).await?;11 ctx.execute_task(send_sms, notification.phone).await?;1213 Ok(NotificationResult { sent: true })14}1516// Level 2 - Notify customer via multiple channels17#[workflow(name = "notify_customer", version = "1.0.0")]18pub async fn notify_customer(19 ctx: WorkflowContext,20 // Workflows take a single input; pass multiple values as a tuple/struct.21 request: (String, String),22) -> Result<CustomerNotificationResult> {23 let (customer_id, message) = request;2425 // Get customer preferences26 let preferences = ctx27 .execute_task(get_customer_preferences, customer_id.clone())28 .await?;2930 // Spawn notification workflow31 let notification = NotificationData {32 email: preferences.email,33 phone: preferences.phone,34 content: message,35 };3637 let handle = ctx38 .start_child_workflow("send_notification", notification)39 .await?;4041 let result = handle.result::<NotificationResult>().await?;4243 Ok(CustomerNotificationResult {44 customer_id,45 notified: result.sent,46 })47}4849// Level 1 - Top-level orchestrator50#[workflow(name = "order_confirmation", version = "1.0.0")]51pub async fn order_confirmation(52 ctx: WorkflowContext,53 order: OrderData,54) -> Result<ConfirmationResult> {55 // Process order56 ctx.execute_task(finalize_order, order.id.clone()).await?;5758 // Notify customer (spawns child -> grandchild)59 let message = format!("Your order {} has been confirmed", order.id);6061 let notification_handle = ctx62 .start_child_workflow(63 "notify_customer",64 (order.customer_id.clone(), message),65 )66 .await?;6768 notification_handle.result::<CustomerNotificationResult>().await?;6970 Ok(ConfirmationResult {71 order_id: order.id,72 confirmed: true,73 })74}