Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically set schedule in Azure Function

I have following function.json for my Azure function whose schedule is set to run at 9.30 daily. What I want is to dynamically set the schedule attribute of this json. This scenario arises when a client who is using my application enters date, on that date-scheduler should run.

   {
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 30 9 * * *" //Want to set dynamically
    }
  ],
  "disabled": false
}

Is this possible?

(Also note, I don't want to use Azure Scheduler due to budgeting reasons)

like image 271
Karan Desai Avatar asked Aug 08 '17 09:08

Karan Desai


People also ask

Can Azure Functions run on a schedule?

One of the triggers for Azure Functions is the timer-trigger – allowing you to run a function on a schedule. With the timer trigger, you can use cron-expression to define when the function needs to run.

How do I trigger Azure function every 5 minutes?

The TimerInfo object is passed into the function. The following example function triggers and executes every five minutes. The @TimerTrigger annotation on the function defines the schedule using the same string format as CRON expressions. The following example shows a timer trigger binding in a function.

How do I create a timer trigger in Azure function?

Create a timer triggered functionIn your function app, select Functions, and then select + Create. Select the Timer trigger template. Configure the new trigger with the settings as specified in the table below the image, and then select Create. Defines the name of your timer triggered function.

Can Azure function have two triggers?

An Azure function can have multiple triggers, but the function must be explicitly configured to use multiple triggers.


1 Answers

Its an old question but still relevant. I recently came across a similar issue. Azure function has a built in functionality that you can use. Its called Eternal orchestrations in Durable Functions (Azure Functions). You can do something like

[FunctionName("Periodic_Cleanup_Loop")]
public static async Task Run([OrchestrationTrigger] IDurableOrchestrationContext 
context)
{

await context.CallActivityAsync("DoCleanup", null);

// sleep for one hour between cleanups
DateTime nextCleanup = context.CurrentUtcDateTime.AddHours(1);
await context.CreateTimer(nextCleanup, CancellationToken.None);

context.ContinueAsNew(null);
}

More lnfo can be found at https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?tabs=csharp

like image 99
Waqas ali Avatar answered Oct 14 '22 00:10

Waqas ali