在 Azure 函数中动态设置计划

Dynamically set schedule in Azure Function

我的 Azure 函数有以下 function.json,其时间表设置为每天 9 点 30 分 运行。我想要的是动态设置这个json的schedule属性。当使用我的应用程序的客户输入日期时,会出现这种情况,该日期调度程序应该 运行。

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

这可能吗?

(另请注意,由于预算原因,我不想使用 Azure Scheduler)

  1. 使用KuduAPI改变function.json https://github.com/projectkudu/kudu/wiki/REST-API

    PUT https://{functionAppName}.scm.azurewebsites.net/api/vfs/{pathToFunction.json}, Headers: If-Match: "*", Body: 新增function.json 内容

  2. 然后发送应用更改的请求

    POST https://{functionAppName}.scm.azurewebsites.net/api/functions/synctriggers

或者您可以使用 Queue 触发器和 "initialVisibilityDelay" 消息。在这种情况下,您需要编写自己的代码来实现调度程序。

您可以修改 function.json 以从应用设置中获取 cron 表达式。

"schedule":“%TriggerSchedule%”

在您的应用程序设置中定义 TriggerSchedule。您可以动态修改您的应用程序设置,函数触发器将与之对齐。

这是一个老问题,但仍然相关。我最近遇到了类似的问题。 Azure 函数具有您可以使用的内置功能。它在 Durable Functions (Azure Functions) 中称为永恒编排。 你可以这样做

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

可以在 https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?tabs=csharp

找到更多信息