Soru

Zorluk: OrtaImplement Durable Functions

You are developing a C# (.NET Isolated process) Durable Function orchestrator named BillingReminderOrchestrator to implement a customer billing dunning process. If a credit card payment fails, the orchestrator must pause execution and wait exactly three days before invoking an activity function to retry the payment. Which code snippet should you use inside the orchestrator to implement this delay?

  1. A
    await Task.Delay(TimeSpan.FromDays(3));
  2. B
    await context.CreateTimer(DateTime.UtcNow.AddDays(3), CancellationToken.None);
  3. await context.CreateTimer(context.CurrentUtcDateTime.AddDays(3), CancellationToken.None);Cevap
  4. D
    Thread.Sleep(TimeSpan.FromDays(3));

Cevap

The correct snippet is the one that calls context.CreateTimer with context.CurrentUtcDateTime.AddDays(3).
The correct snippet uses context.CreateTimer alongside context.CurrentUtcDateTime. Durable orchestrators must be completely deterministic. Standard system-clock APIs like DateTime.UtcNow are non-deterministic during orchestration replays, so developers must use the context-provided CurrentUtcDateTime. Thread-blocking methods like Task.Delay and Thread.Sleep are also forbidden because they consume resources unnecessarily instead of scheduling a durable task in the execution history.

Adım Adım Çözüm

1
Analyze the orchestrator's requirements and constraints.
The orchestrator needs to wait three days. However, standard .NET sleep/delay APIs and clock APIs violate the determinism constraint of Durable Functions.
Durable Functions orchestrators run by executing code, recording checkpoints, and replaying history. Any non-deterministic value or thread-blocking operation causes runtime issues during replay.
2
Evaluate the date-time retrieval method.
Using DateTime.UtcNow is non-deterministic because it returns a different value on every execution, whereas context.CurrentUtcDateTime is deterministic because it retrieves a stable timestamp from the execution history.
Choosing the context-provided CurrentUtcDateTime ensures replay operations do not result in a different path or exception.
3
Select the correct asynchronous delay method.
Using context.CreateTimer schedules a durable timer task that yields execution back to the Azure Functions runtime rather than blocking the host worker thread.
This allows the function app to scale down to zero or handle other processes while waiting, and resumes the orchestrator once the timer fires.

Anahtar Kavram

Orchestrator determinism and durable timers
Tahmini Süre:1m 30s
Bu soruyu puanla