Guides

Human-in-the-Loop Approvals


Human-in-the-Loop Approvals

Many business processes require a human to make a decision before the workflow can proceed. Orcher's wait_for_event_with_timeout lets you pause the workflow until a signal arrives (e.g., an approval or rejection), with an automatic timeout that triggers escalation or a default action if no one responds in time.

Submit Request
Notify Approver
wait_for_event("approval")
timeout: 48 hours ยท zero resources
approved
Process
Execute action
rejected
Cancel
Notify requester
timeout
Escalate
Notify manager
1
use orcher::prelude::*;
2
use anyhow::Result;
3
use std::time::Duration;
4
5
#[derive(Debug, Clone, Serialize, Deserialize)]
6
struct ApprovalRequest {
7
request_id: String,
8
requester: String,
9
amount: f64,
10
description: String,
11
}
12
13
#[derive(Debug, Clone, Serialize, Deserialize)]
14
struct ApprovalResult {
15
request_id: String,
16
approved: bool,
17
approver: Option<String>,
18
decision_time: String,
19
escalated: bool,
20
}
21
22
#[derive(Debug, Clone, Serialize, Deserialize)]
23
struct ApprovalDecision {
24
approved: bool,
25
approver: String,
26
comments: String,
27
}
28
29
#[task]
30
async fn notify_approver(request: ApprovalRequest) -> Result<()> {
31
// Send notification to the approver
32
Ok(())
33
}
34
35
#[task]
36
async fn escalate_to_manager(request: ApprovalRequest) -> Result<()> {
37
// Escalate to manager
38
Ok(())
39
}
40
41
// Approval workflow with timeout and escalation
42
#[workflow(name = "approval_workflow", version = "1.0.0")]
43
pub async fn approval_workflow(
44
ctx: WorkflowContext,
45
request: ApprovalRequest,
46
) -> Result<ApprovalResult> {
47
println!("Approval request: {} - ${}", request.request_id, request.amount);
48
49
// Send notification to approver
50
ctx.execute_task(notify_approver, request.clone()).await?;
51
52
// Wait for approval with 48-hour timeout
53
let decision: Option<ApprovalDecision> = ctx
54
.wait_for_event_with_timeout(
55
"approval_decision",
56
Duration::from_secs(172800) // 48 hours
57
)
58
.await?;
59
60
match decision {
61
Some(decision) => {
62
println!("Decision received from {}: {}",
63
decision.approver,
64
if decision.approved { "APPROVED" } else { "REJECTED" }
65
);
66
67
Ok(ApprovalResult {
68
request_id: request.request_id,
69
approved: decision.approved,
70
approver: Some(decision.approver),
71
decision_time: chrono::Utc::now().to_rfc3339(),
72
escalated: false,
73
})
74
}
75
None => {
76
println!("Approval timeout - escalating to manager");
77
78
// Escalate to manager
79
ctx.execute_task(escalate_to_manager, request.clone()).await?;
80
81
// Wait for manager decision (24 hours)
82
let manager_decision: Option<ApprovalDecision> = ctx
83
.wait_for_event_with_timeout(
84
"manager_decision",
85
Duration::from_secs(86400)
86
)
87
.await?;
88
89
match manager_decision {
90
Some(decision) => Ok(ApprovalResult {
91
request_id: request.request_id,
92
approved: decision.approved,
93
approver: Some(decision.approver),
94
decision_time: chrono::Utc::now().to_rfc3339(),
95
escalated: true,
96
}),
97
None => {
98
// Auto-reject after escalation timeout
99
println!("Manager timeout - auto-rejecting");
100
Ok(ApprovalResult {
101
request_id: request.request_id,
102
approved: false,
103
approver: None,
104
decision_time: chrono::Utc::now().to_rfc3339(),
105
escalated: true,
106
})
107
}
108
}
109
}
110
}
111
}

Next Steps