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
1use orcher::prelude::*;2use anyhow::Result;3use std::time::Duration;45#[derive(Debug, Clone, Serialize, Deserialize)]6struct ApprovalRequest {7 request_id: String,8 requester: String,9 amount: f64,10 description: String,11}1213#[derive(Debug, Clone, Serialize, Deserialize)]14struct ApprovalResult {15 request_id: String,16 approved: bool,17 approver: Option<String>,18 decision_time: String,19 escalated: bool,20}2122#[derive(Debug, Clone, Serialize, Deserialize)]23struct ApprovalDecision {24 approved: bool,25 approver: String,26 comments: String,27}2829#[task]30async fn notify_approver(request: ApprovalRequest) -> Result<()> {31 // Send notification to the approver32 Ok(())33}3435#[task]36async fn escalate_to_manager(request: ApprovalRequest) -> Result<()> {37 // Escalate to manager38 Ok(())39}4041// Approval workflow with timeout and escalation42#[workflow(name = "approval_workflow", version = "1.0.0")]43pub async fn approval_workflow(44 ctx: WorkflowContext,45 request: ApprovalRequest,46) -> Result<ApprovalResult> {47 println!("Approval request: {} - ${}", request.request_id, request.amount);4849 // Send notification to approver50 ctx.execute_task(notify_approver, request.clone()).await?;5152 // Wait for approval with 48-hour timeout53 let decision: Option<ApprovalDecision> = ctx54 .wait_for_event_with_timeout(55 "approval_decision",56 Duration::from_secs(172800) // 48 hours57 )58 .await?;5960 match decision {61 Some(decision) => {62 println!("Decision received from {}: {}",63 decision.approver,64 if decision.approved { "APPROVED" } else { "REJECTED" }65 );6667 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");7778 // Escalate to manager79 ctx.execute_task(escalate_to_manager, request.clone()).await?;8081 // Wait for manager decision (24 hours)82 let manager_decision: Option<ApprovalDecision> = ctx83 .wait_for_event_with_timeout(84 "manager_decision",85 Duration::from_secs(86400)86 )87 .await?;8889 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 timeout99 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}