Guides

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
1
use orcher::prelude::*;
2
use anyhow::Result;
3
4
// Level 3 - Leaf operation
5
#[workflow(name = "send_notification", version = "1.0.0")]
6
pub 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?;
12
13
Ok(NotificationResult { sent: true })
14
}
15
16
// Level 2 - Notify customer via multiple channels
17
#[workflow(name = "notify_customer", version = "1.0.0")]
18
pub 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;
24
25
// Get customer preferences
26
let preferences = ctx
27
.execute_task(get_customer_preferences, customer_id.clone())
28
.await?;
29
30
// Spawn notification workflow
31
let notification = NotificationData {
32
email: preferences.email,
33
phone: preferences.phone,
34
content: message,
35
};
36
37
let handle = ctx
38
.start_child_workflow("send_notification", notification)
39
.await?;
40
41
let result = handle.result::<NotificationResult>().await?;
42
43
Ok(CustomerNotificationResult {
44
customer_id,
45
notified: result.sent,
46
})
47
}
48
49
// Level 1 - Top-level orchestrator
50
#[workflow(name = "order_confirmation", version = "1.0.0")]
51
pub async fn order_confirmation(
52
ctx: WorkflowContext,
53
order: OrderData,
54
) -> Result<ConfirmationResult> {
55
// Process order
56
ctx.execute_task(finalize_order, order.id.clone()).await?;
57
58
// Notify customer (spawns child -> grandchild)
59
let message = format!("Your order {} has been confirmed", order.id);
60
61
let notification_handle = ctx
62
.start_child_workflow(
63
"notify_customer",
64
(order.customer_id.clone(), message),
65
)
66
.await?;
67
68
notification_handle.result::<CustomerNotificationResult>().await?;
69
70
Ok(ConfirmationResult {
71
order_id: order.id,
72
confirmed: true,
73
})
74
}

Next Steps