Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1277,17 +1277,26 @@ func PurchaseOrderWorkflow(ctx *workflow.WorkflowContext) (any, error) {
return "Auto-approved", nil
}
// Orders of $1000 or more require manager approval
if err := ctx.CallActivity(SendApprovalRequest, workflow.ActivityInput(order)).Await(nil); err != nil {
if err := ctx.CallActivity(SendApprovalRequest, workflow.WithActivityInput(order)).Await(nil); err != nil {
return "", err
}
// Approvals must be received within 24 hours or they will be cancelled
approvalEvent := ctx.WaitForExternalEvent("approval_received", time.Hour*24)
timeoutEvent := ctx.CreateTimer(time.Hour * 24)
winner, err := ctx.Select(approvalEvent, timeoutEvent)
if err != nil {
return "", err
}
if winner == 1 {
// The timer won the race: no approval arrived in time
return "Cancelled", nil
}
Comment on lines +1284 to +1293

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this Select example is the right one, since WaitForExternalEvent already have (and the same!) timeout. Is there another Select example we could do instead?

var approval Approval
if err := ctx.WaitForExternalEvent("approval_received", time.Hour*24).Await(&approval); err != nil {
// Assuming that a timeout has taken place - in any case; an error.
return "error/cancelled", err
if err := approvalEvent.Await(&approval); err != nil {
return "", err
}
// The order was approved
if err := ctx.CallActivity(PlaceOrder, workflow.ActivityInput(order)).Await(nil); err != nil {
if err := ctx.CallActivity(PlaceOrder, workflow.WithActivityInput(order)).Await(nil); err != nil {
return "", err
}
return fmt.Sprintf("Approved by %s", approval.Approver), nil
Expand Down