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
1use orcher::prelude::*;2use anyhow::Result;3use std::time::Duration;45// Child workflow that can be expedited6#[workflow(name = "fulfillment_workflow", version = "1.0.0")]7pub async fn fulfillment_workflow(8 ctx: WorkflowContext,9 order_id: String,10) -> Result<FulfillmentResult> {11 println!("Starting standard fulfillment for {}", order_id);1213 // Check if expedite event is received14 let expedite: Option<bool> = ctx15 .wait_for_event_with_timeout("expedite", Duration::from_secs(300))16 .await?;1718 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 }2526 Ok(FulfillmentResult { completed: true })27}2829// Parent workflow that controls child30#[workflow(name = "order_orchestrator", version = "1.0.0")]31pub async fn order_orchestrator(32 ctx: WorkflowContext,33 order: OrderData,34) -> Result<OrderResult> {35 // Start child fulfillment workflow36 let child_handle = ctx37 .start_child_workflow("fulfillment_workflow", order.id.clone())38 .await?;3940 println!("Child workflow started: {}", child_handle.id());4142 // Wait for external expedite request43 let expedite: Option<bool> = ctx44 .wait_for_event_with_timeout("expedite_request", Duration::from_secs(60))45 .await?;4647 if expedite.unwrap_or(false) {48 println!("Expedite requested - notifying child workflow");4950 // NOTE: sending events to a child from inside a workflow via the child51 // handle is not yet implemented in the Rust SDK52 // (`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 }5758 // Wait for child to complete59 let result = child_handle.result::<FulfillmentResult>().await?;6061 Ok(OrderResult {62 order_id: order.id,63 expedited: expedite.unwrap_or(false),64 completed: result.completed,65 })66}Send expedite event from client:
1use orcher::client::Client;23async fn expedite_order(workflow_id: &str) -> Result<()> {4 let client = Client::connect("http://localhost:50051").await?;56 // Get a handle to the running parent workflow, then send it an event7 let handle = client.get_workflow_handle(workflow_id).await?;8 handle.send_event("expedite_request", true).await?;910 println!("Expedite request sent");11 Ok(())12}