1

I have this function:

[FunctionName("MultipleTimersOrchestrator")]
        public async Task<List<string>> RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            var outputs = new List<string>();

            log.LogInformation($"Starting process {DateTime.Now}");

            DateTime deadline = context.CurrentUtcDateTime.Add(TimeSpan.FromSeconds(5));
            await context.CreateTimer(deadline, CancellationToken.None);

            log.LogInformation($"Starting GetUser Tokyo {DateTime.Now}");
            outputs.Add(await context.CallActivityAsync<string>("GetUser", "Tokyo"));
            log.LogInformation($"Ended GetUser Tokyo {DateTime.Now}");

            DateTime deadline2 = context.CurrentUtcDateTime.Add(TimeSpan.FromSeconds(10));
            await context.CreateTimer(deadline2, CancellationToken.None);

            log.LogInformation($"Starting GetUser Seattle {DateTime.Now} ");
            await context.CallActivityAsync<string>("GetUser", "Seattle");
            log.LogInformation($"Ended GetUser Seattle {DateTime.Now}");


            return outputs;
        }

My problem is that it is acting weird...Is it possible to have more than a timer awaited in one orchestratror function? It seems, by the log, that the whole funtion is being executed after 5, and then after 10 seconds. I was expecting it to work like a Task.Delay() Is that correct?Someone can explain that?

Thanks a lot.

1 Answer 1

1

To clarify, using multiple create timer calls is a very valid pattern, and it will function fairly similar to a Task.Delay(), though over the course of several function executions.

It appears that what is confusing you is the fact that Orchestrator functions replay functions everytime they call await on one of the IDurableOrchestrationContext API methods. This is how Durable Functions achieves the "durability" behind its name.

This can be very confusing for new developers, especially with logging. I would recommend using our replay-safe logger, which will not log messages that were already logged in previous replays. You can do that by adding the following to the top of your orchestration function.

log = context.CreateReplaySafeLogger(log);

You can partially curb this replay behavior by using extended sessions, which functionally allow the orchestration to resume where it left off assuming the messages that it calls await on return in a timely fashion.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Connor, you are totally right, great explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.