Reputation: 3142
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)
Upvotes: 16
Views: 9466
Reputation: 278
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://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?tabs=csharp
Upvotes: 6
Reputation: 1322
You could modify your function.json to fetch the cron expression from app settings.
"schedule": "%TriggerSchedule%"
Define TriggerSchedule in your appsettings. You could modify your appsettings dynamically and the function trigger would align to it.
Upvotes: 12
Reputation: 1446
Use Kudu API to change function.json https://github.com/projectkudu/kudu/wiki/REST-API
PUT https://{functionAppName}.scm.azurewebsites.net/api/vfs/{pathToFunction.json}, Headers: If-Match: "*", Body: new function.json content
Then send request to apply changes
POST https://{functionAppName}.scm.azurewebsites.net/api/functions/synctriggers
Or you can use Queue Trigger with "initialVisibilityDelay" messages. In this case you need to write your own code to implement a scheduler.
Upvotes: 5