Soru

Zorluk: ZorImplement Durable Functions

You are developing a software release pipeline using Azure Durable Functions. The workflow is written in C# (.NET Isolated process) and must implement a gated approval step. The orchestrator must send an approval notification to a release manager, wait for up to 24 hours for a response via an external event, and then either proceed with the deployment or cancel it. If the manager responds before the timeout, the workflow must prevent the timer from running to completion to avoid unnecessary execution.

Move the steps required to implement this gated approval pattern within the orchestrator function into the correct execution sequence.

  1. 1Call the activity function to send the approval request notification to the manager.
  2. 2Create a CancellationTokenSource, start a durable timer task using the token, and start an external event listener task.
  3. 3Block execution by calling Task.WhenAny on both the durable timer and external event tasks.
  4. 4Cancel the durable timer using the CancellationTokenSource if the external event task resolved first.
  5. 5Determine the workflow outcome (approved, rejected, or timed out) and call the corresponding deployment or cleanup activity.

Cevap

The correct sequence begins with notifying the manager, setting up the timer and event tasks, waiting for either task to resolve, canceling the timer if the event is received first, and finally triggering the appropriate deployment or cleanup activity.
In a C# Durable Functions orchestrator implementing a human interaction pattern with a timeout, the orchestrator first calls an activity to send the notification. Next, it must define the timer (linked to a cancellation token) and the external event listener. It then blocks execution using Task.WhenAny to wait for whichever occurs first. If the event occurs first, it is critical to cancel the timer using the CancellationTokenSource to prevent it from executing at a later time. Finally, the orchestrator calls the corresponding activity based on the outcome.

Adım Adım Çözüm

1
Call the activity function to send the approval request notification.
The manager is notified, and the workflow is ready to wait for input.
This initiates the human interaction process.
2
Create a CancellationTokenSource and instantiate both the durable timer and the external event tasks.
Two unresolved Task objects represent the timer and the external event.
Both tasks must exist in memory before they can be evaluated concurrently.
3
Await Task.WhenAny with both the timer and event tasks as inputs.
The orchestrator yields and remains suspended until either the timer expires or the external event is received.
This allows the orchestrator to resume immediately upon the first completed action.
4
Check if the external event task resolved first; if so, call Cancel on the CancellationTokenSource.
The durable timer is canceled in the Azure Functions backend.
This prevents the timer from firing, which is a best practice to avoid billing overhead and storage leaks.
5
Examine the result of the completed task and call either the deployment or cleanup activity.
The deployment is executed if approved, or the release is cancelled/notified if rejected or timed out.
This completes the lifecycle of the approval gate workflow.

Anahtar Kavram

Implementing the Human Interaction pattern with timeouts in Azure Durable Functions using CancellationTokenSource and Task.WhenAny.
Tahmini Süre:2m 30s
Bu soruyu puanla