Dynamically set schedule in Azure Function

2019-05-07 03:40发布

问题:

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)

回答1:

  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.



回答2:

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.