Pramod J C
Pramod J C

Reputation: 13

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?

Upvotes: 0

Views: 2098

Answers (2)

Jarko951
Jarko951

Reputation: 48

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}");
            }

        }

 

Upvotes: 2

user1672994
user1672994

Reputation: 10849

You can define timer-triggered function with DurableOrchestrationClient input binding. Please see below sample declaration:

[FunctionName("TimerDurableFunctionStarter")]
public static async Task Run(
    [TimerTrigger("0 */1 * * * *")] TimerInfo info,
    [DurableClient] IDurableOrchestrationClient timerDurableOrchestratorStarter)
{
   string instanceId = await timerDurableOrchestratorStarter.StartNewAsync("<<OrchestratorFunctionName>>");
}

Upvotes: 1

Related Questions