azure-functionsscheduled-tasksazure-durable-functionsazure-http-triggertimer-trigger

Timer triggered Durable function schedule


I am currently building an azure durable function for my organization. The requirement is to run this orchestration every weekday during midnight. With eternal functions we can only provide a delay. How can I achieve this goal through a cron expression as such?

Can I create a Timer triggered Durable function? Is there any limitation? Or should I create a HTTP Triggered durable function in which the orchestrator waits for an external event; and then have a normal timer trigger azure function raise that event according to the Cron expression?


Solution

  • If using dotnet isolated durable function you have to change the type of the client to DurableTaskClient.

    Example timer that triggers an orchestration(when moving to production, consider changing 'RunOnStartup' to false to avoid unwanted effects on function scaling).

         [Function("TimerStart")]
            public async Task TimerStart(
             [TimerTrigger("0 */15 * * * *", RunOnStartup = true)] TimerInfo myTimer,
               [DurableClient] DurableTaskClient client,
               FunctionContext executionContext)
            {
                ILogger logger = executionContext.GetLogger("TimerStart");
    
                string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
                    nameof(MyOrchestration));
    
                logger.LogInformation("Timer started orchestration with ID = '{instanceId}'.", instanceId);
    
                if (myTimer.ScheduleStatus is not null)
                {
                    logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
                }
    
            }