0

I want to wait for an external event and send a reminder if the event hasn't occurred in 5 days. I am new to how tasks work. Will the following orchestrator function run as expected? I didn't use Task.WhenAll as I though this way was simpler:

[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
}

Do I need to use Task.Run instead of just directly calling SendReminder?

Kurren
  • 827
  • 1
  • 9
  • 18
  • No, I don't think you need `Task.Run`. If anything you might want to `await SendReminder` but in this instance I don't think you need/want to do that either. Nothing is as asuring as a unit test. ;-) – phil Jul 09 '22 at 17:49
  • @phil thanks. I’ve read that usually you should use `Task.Run` or `configureAwait` instead of just calling the function directly because it might cause deadlocks: https://stackoverflow.com/questions/53183370/c-sharp-how-to-start-an-async-method-without-await-its-complete. But I know durable functions work differently as it completely stops execution on await? – Kurren Jul 10 '22 at 08:26

0 Answers0