Order Fulfillment Process
Order Fulfillment Process
Order fulfillment is a multi-day process with distinct stages — payment processing, warehouse picking, packing, shipping, and delivery confirmation. Each stage may involve waiting for external systems or physical operations. Orcher handles this naturally by sleeping between stages and waiting for delivery confirmation signals with automatic timeouts.
1
Process Payment
Charge card · reserve funds
2
Reserve Inventory
Warehouse DB update
3
Pick & Pack
Warehouse fulfillment
wait_for_event("packed") · timeout 24h
3
Ship Order
Generate label · hand to carrier
wait_for_event("delivered") · timeout 7d
Confirm Delivery
Delivered
Send receipt · close order
Timeout
Investigate · contact carrier
1use orcher::prelude::*;2use anyhow::Result;3use std::time::Duration;45#[derive(Debug, Clone, Serialize, Deserialize)]6struct Order {7 order_id: String,8 customer_id: String,9 items: Vec<String>,10 total: f64,11}1213#[derive(Debug, Clone, Serialize, Deserialize)]14struct FulfillmentResult {15 order_id: String,16 status: String,17 days_taken: u64,18 checkpoints: Vec<String>,19}2021#[task]22async fn validate_order(order: Order) -> Result<()> { Ok(()) }2324#[task]25async fn charge_payment(total: f64) -> Result<()> { Ok(()) }2627#[task]28async fn warehouse_pick(items: Vec<String>) -> Result<bool> { Ok(true) }2930#[task]31async fn pack_items(order_id: String) -> Result<()> { Ok(()) }3233#[task]34async fn create_shipment(order: Order) -> Result<String> { Ok("TRACK-123".to_string()) }3536#[task]37async fn send_delivery_notification(customer_id: String) -> Result<()> { Ok(()) }3839// Multi-day order fulfillment workflow40#[workflow(name = "order_fulfillment", version = "1.0.0")]41pub async fn order_fulfillment(42 ctx: WorkflowContext,43 order: Order,44) -> Result<FulfillmentResult> {45 let start = chrono::Utc::now();46 let mut checkpoints = Vec::new();4748 println!("Order {} - Starting fulfillment", order.order_id);4950 // Day 0: Process order51 ctx.execute_task(validate_order, order.clone()).await?;52 ctx.execute_task(charge_payment, order.total).await?;53 checkpoints.push("order_processed".to_string());5455 // Day 0: Wait for warehouse to pick items (typically same day)56 ctx.sleep(Duration::from_secs(14400)).await?; // 4 hours5758 let pick_result: bool = ctx59 .execute_task(warehouse_pick, order.items.clone())60 .await?;6162 if !pick_result {63 // Wait for inventory restock (could take days)64 ctx.sleep(Duration::from_secs(259200)).await?; // 3 days65 ctx.execute_task(warehouse_pick, order.items.clone()).await?;66 }67 checkpoints.push("items_picked".to_string());6869 // Day 1: Pack and ship70 ctx.sleep(Duration::from_secs(86400)).await?; // 1 day71 ctx.execute_task(pack_items, order.order_id.clone()).await?;72 let _tracking_id: String = ctx.execute_task(create_shipment, order.clone()).await?;73 checkpoints.push("shipped".to_string());7475 // Day 1-5: Transit time76 ctx.sleep(Duration::from_secs(345600)).await?; // 4 days7778 // Day 5: Delivery confirmation79 let delivered: Option<bool> = ctx80 .wait_for_event_with_timeout(81 "delivery_confirmed",82 Duration::from_secs(86400) // 1 day grace period83 )84 .await?;8586 if delivered.unwrap_or(false) {87 checkpoints.push("delivered".to_string());88 ctx.execute_task(send_delivery_notification, order.customer_id.clone()).await?;89 } else {90 println!("Delivery not confirmed - marking as complete anyway");91 checkpoints.push("delivery_uncertain".to_string());92 }9394 let days_taken = (chrono::Utc::now() - start).num_days() as u64;9596 Ok(FulfillmentResult {97 order_id: order.order_id,98 status: "completed".to_string(),99 days_taken,100 checkpoints,101 })102}