-
I want to wait for an external event and send a reminder if the event hasn't occurred in 5 days. Sending the reminder will be a "fire and forget" task. I am new to how tasks work. Will the following orchestrator function be okay? I didn't use [FunctionName("Function1")]
public static async Task RunOrchestrator(
[OrchestrationTrigger] IDurableOrchestrationContext context)
{
using (var cts = new CancellationTokenSource())
{
SendReminder(context, cts.Token); // dont await so this happens in the background
await context.WaitForExternalEvent("MyEvent");
cts.Cancel(); // cancel the reminder as the event has now happened
// react to MyEvent ...
}
}
public static async Task SendReminder(IDurableOrchestrationContext context, CancellationToken cancellationToken)
{
await context.CreateTimer(context.CurrentUtcDateTime.AddDays(5), cancellationToken);
await context.CallHttpAsync(...) // http call to ms graph to send a reminder email
} I have read the code constraints article so I know not to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I think this should be okay. I need to be reminded about what the behavior is when cancelling a timer (it might result in a I don't think this applies to your example here, but one behevior to be aware of is that all outstanding tasks must be completed or canceled before an orchestration can transition into a |
Beta Was this translation helpful? Give feedback.
I think this should be okay. I need to be reminded about what the behavior is when cancelling a timer (it might result in a
TaskCancelledException
at the point ofawait context.CreateTimer(...)
, but otherwise I think what you have is fine.I don't think this applies to your example here, but one behevior to be aware of is that all outstanding tasks must be completed or canceled before an orchestration can transition into a
Completed
state, so if you execute a task, don't await it, and let the function complete, you might be surprised to see that your orchestration remains in theRunning
state until the non-awaited tasks are completed.