Guides

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
1
use orcher::prelude::*;
2
use anyhow::Result;
3
use std::time::Duration;
4
5
#[derive(Debug, Clone, Serialize, Deserialize)]
6
struct Order {
7
order_id: String,
8
customer_id: String,
9
items: Vec<String>,
10
total: f64,
11
}
12
13
#[derive(Debug, Clone, Serialize, Deserialize)]
14
struct FulfillmentResult {
15
order_id: String,
16
status: String,
17
days_taken: u64,
18
checkpoints: Vec<String>,
19
}
20
21
#[task]
22
async fn validate_order(order: Order) -> Result<()> { Ok(()) }
23
24
#[task]
25
async fn charge_payment(total: f64) -> Result<()> { Ok(()) }
26
27
#[task]
28
async fn warehouse_pick(items: Vec<String>) -> Result<bool> { Ok(true) }
29
30
#[task]
31
async fn pack_items(order_id: String) -> Result<()> { Ok(()) }
32
33
#[task]
34
async fn create_shipment(order: Order) -> Result<String> { Ok("TRACK-123".to_string()) }
35
36
#[task]
37
async fn send_delivery_notification(customer_id: String) -> Result<()> { Ok(()) }
38
39
// Multi-day order fulfillment workflow
40
#[workflow(name = "order_fulfillment", version = "1.0.0")]
41
pub 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();
47
48
println!("Order {} - Starting fulfillment", order.order_id);
49
50
// Day 0: Process order
51
ctx.execute_task(validate_order, order.clone()).await?;
52
ctx.execute_task(charge_payment, order.total).await?;
53
checkpoints.push("order_processed".to_string());
54
55
// Day 0: Wait for warehouse to pick items (typically same day)
56
ctx.sleep(Duration::from_secs(14400)).await?; // 4 hours
57
58
let pick_result: bool = ctx
59
.execute_task(warehouse_pick, order.items.clone())
60
.await?;
61
62
if !pick_result {
63
// Wait for inventory restock (could take days)
64
ctx.sleep(Duration::from_secs(259200)).await?; // 3 days
65
ctx.execute_task(warehouse_pick, order.items.clone()).await?;
66
}
67
checkpoints.push("items_picked".to_string());
68
69
// Day 1: Pack and ship
70
ctx.sleep(Duration::from_secs(86400)).await?; // 1 day
71
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());
74
75
// Day 1-5: Transit time
76
ctx.sleep(Duration::from_secs(345600)).await?; // 4 days
77
78
// Day 5: Delivery confirmation
79
let delivered: Option<bool> = ctx
80
.wait_for_event_with_timeout(
81
"delivery_confirmed",
82
Duration::from_secs(86400) // 1 day grace period
83
)
84
.await?;
85
86
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
}
93
94
let days_taken = (chrono::Utc::now() - start).num_days() as u64;
95
96
Ok(FulfillmentResult {
97
order_id: order.order_id,
98
status: "completed".to_string(),
99
days_taken,
100
checkpoints,
101
})
102
}

Next Steps