Guides

Parent-Child Event Coordination


Parent-Child Event Coordination

Parents can send events (signals) to child workflows to dynamically control their behavior at runtime. This enables patterns like expediting an order, canceling a long-running child, or updating a child's configuration without restarting it. The child calls wait_for_event to pause until an event arrives, and the parent (or an external client) sends the event via the child's handle.

Parent
Start child
Do other work
send_event
"expedite"
Await result
►
►
Child
own execution
Process order
wait_for_event
paused · zero resources
Expedite shipping
event received
Return result
1
use orcher::prelude::*;
2
use anyhow::Result;
3
use std::time::Duration;
4
5
// Child workflow that can be expedited
6
#[workflow(name = "fulfillment_workflow", version = "1.0.0")]
7
pub async fn fulfillment_workflow(
8
ctx: WorkflowContext,
9
order_id: String,
10
) -> Result<FulfillmentResult> {
11
println!("Starting standard fulfillment for {}", order_id);
12
13
// Check if expedite event is received
14
let expedite: Option<bool> = ctx
15
.wait_for_event_with_timeout("expedite", Duration::from_secs(300))
16
.await?;
17
18
if expedite.unwrap_or(false) {
19
println!("Expedited fulfillment requested!");
20
ctx.execute_task(expedited_shipping, order_id).await?;
21
} else {
22
println!("Standard fulfillment");
23
ctx.execute_task(standard_shipping, order_id).await?;
24
}
25
26
Ok(FulfillmentResult { completed: true })
27
}
28
29
// Parent workflow that controls child
30
#[workflow(name = "order_orchestrator", version = "1.0.0")]
31
pub async fn order_orchestrator(
32
ctx: WorkflowContext,
33
order: OrderData,
34
) -> Result<OrderResult> {
35
// Start child fulfillment workflow
36
let child_handle = ctx
37
.start_child_workflow("fulfillment_workflow", order.id.clone())
38
.await?;
39
40
println!("Child workflow started: {}", child_handle.id());
41
42
// Wait for external expedite request
43
let expedite: Option<bool> = ctx
44
.wait_for_event_with_timeout("expedite_request", Duration::from_secs(60))
45
.await?;
46
47
if expedite.unwrap_or(false) {
48
println!("Expedite requested - notifying child workflow");
49
50
// NOTE: sending events to a child from inside a workflow via the child
51
// handle is not yet implemented in the Rust SDK
52
// (`ChildWorkflowHandle::send_event` returns an unimplemented error).
53
// Deliver the event to the child from an external client instead,
54
// addressing it by the child's workflow id.
55
child_handle.send_event("expedite", true).await?;
56
}
57
58
// Wait for child to complete
59
let result = child_handle.result::<FulfillmentResult>().await?;
60
61
Ok(OrderResult {
62
order_id: order.id,
63
expedited: expedite.unwrap_or(false),
64
completed: result.completed,
65
})
66
}

Send expedite event from client:

1
use orcher::client::Client;
2
3
async fn expedite_order(workflow_id: &str) -> Result<()> {
4
let client = Client::connect("http://localhost:50051").await?;
5
6
// Get a handle to the running parent workflow, then send it an event
7
let handle = client.get_workflow_handle(workflow_id).await?;
8
handle.send_event("expedite_request", true).await?;
9
10
println!("Expedite request sent");
11
Ok(())
12
}

Next Steps