Nested Fan-Out/Fan-In
Nested Fan-Out/Fan-In
For hierarchical data — such as regions containing stores, or categories containing products — you can nest fan-out operations. The parent workflow fans out across the top-level groups (e.g., regions), and each group is processed by a child workflow that itself fans out across its members (e.g., stores). Results bubble up through the hierarchy: stores report to regions, regions aggregate into the final report. Use child workflows for the outer fan-out so each group gets independent error handling and its own journal.
Parent Workflow
generate_report
fan-out by region
Region A
child workflow
Store 1
Store 2
Store 3
Region B
child workflow
Store 1
Store 2
Store 3
Region C
child workflow
Store 1
Store 2
Store 3
fan-in
Aggregate Report
1use orcher::prelude::*;2use anyhow::Result;34#[derive(Debug, Clone, Serialize, Deserialize)]5struct Region {6 region_id: String,7 stores: Vec<String>,8}910#[derive(Debug, Clone, Serialize, Deserialize)]11struct RegionalData {12 region_id: String,13 store_count: usize,14 total_sales: f64,15}1617#[derive(Debug, Clone, Serialize, Deserialize)]18struct ReportResult {19 total_regions: usize,20 total_stores: usize,21 total_sales: f64,22}2324// Task to get a single store's sales25#[task(name = "get_store_sales")]26pub async fn get_store_sales(27 _ctx: TaskContext,28 store_id: String,29) -> Result<f64> {30 // Fetch sales data for this store31 Ok(store_id.len() as f64 * 1000.0)32}3334// Child workflow: Level 2 fan-out for stores in a region35#[workflow(name = "process_region", version = "1.0.0")]36pub async fn process_region(37 ctx: WorkflowContext,38 region: Region,39) -> Result<RegionalData> {40 // Level 2 Fan-Out: Process each store in parallel41 let mut store_tasks = Vec::new();4243 for store in ®ion.stores {44 let task = ctx.execute_task(get_store_sales, store.clone());45 store_tasks.push(task);46 }4748 // Level 2 Fan-In: Aggregate store sales49 let mut total_sales = 0.0;5051 for task in store_tasks {52 let sales: f64 = task.await?;53 total_sales += sales;54 }5556 Ok(RegionalData {57 region_id: region.region_id,58 store_count: region.stores.len(),59 total_sales,60 })61}6263// Parent workflow: Level 1 fan-out across regions64#[workflow(name = "regional_report", version = "1.0.0")]65pub async fn regional_report(66 ctx: WorkflowContext,67 regions: Vec<Region>,68) -> Result<ReportResult> {69 println!("Generating report for {} regions", regions.len());7071 // Level 1 Fan-Out: Process each region as a child workflow72 let mut region_handles = Vec::new();7374 for region in ®ions {75 let handle = ctx76 .start_child_workflow("process_region", region.clone())77 .await?;78 region_handles.push(handle);79 }8081 // Level 1 Fan-In: Collect regional results82 let mut total_stores = 0;83 let mut total_sales = 0.0;8485 for handle in region_handles {86 let regional_data: RegionalData = handle.result().await?;87 total_stores += regional_data.store_count;88 total_sales += regional_data.total_sales;89 }9091 println!("Report complete: {} regions, {} stores, ${:.2} total sales",92 regions.len(), total_stores, total_sales);9394 Ok(ReportResult {95 total_regions: regions.len(),96 total_stores,97 total_sales,98 })99}