14

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)

Karan Desai
  • 3,012
  • 5
  • 32
  • 66

3 Answers3

12

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.

Tany
  • 1,252
  • 14
  • 30
  • Interesting. I tried your way, but how can I dynamically modify appsettings of azure portal? Can you provide more details please – Karan Desai Aug 19 '17 at 13:18
  • You could use powershell cmdlet New-AzureRmResource to modify appsettings – Tany Aug 19 '17 at 19:10
5
  1. 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

  2. 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.

Alexey Rodionov
  • 1,436
  • 6
  • 8
  • Thanks for the answer. As soon as I'll try these steps, I'll inform you if they worked for me or not. – Karan Desai Aug 10 '17 at 02:42
  • Hi, I tried following your steps, but it is giving me _401 - Unauthorized: Access is denied due to invalid credentials._ I am using postman for making these api calls. Which credentials to send and where in api call? – Karan Desai Aug 18 '17 at 04:06
  • Wow its done! I sent UserName and Password (of my function app's publish Profiles) as authorization and it worked like charm. Thanks a lot :) – Karan Desai Aug 18 '17 at 04:36
  • Is this configuration update on a user by user basis? Let's say I have two users logged in, user number 1 sets his timer to run the function 60 minutes from now but 30 minutes later another user (user number 2) sets his timer to 150 minutes. Would the timer of user number two affect the 60 minutes timer set by user number 1? I am trying to do something similar to what Karan is doing but I want my timers to be on a user by user basis. Is this even possible? – Adrian Cruz Jan 28 '20 at 15:44
  • @KaranDesai I have a similar requirement like you, and the given solution does make changes to the function.json file, however the changes do not take effect until I manually restart the azure function – Manzur Alahi Feb 24 '22 at 06:29
5

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

Waqas ali
  • 258
  • 3
  • 7
  • 1
    While this could be an viable solution for certain cases, this does not allow you to schedule work as the question asks. What this allows you to do is to trigger an activity periodically after it's completion. – Chamika Goonetilaka Sep 02 '21 at 10:08