如何使用定时触发器配置Azure自定义处理程序?

huangapple go评论84阅读模式
英文:

How to configure an Azure custom handler with a timer trigger?

问题

我正在尝试在我的 Golang 自定义处理程序中配置一个使用定时器触发器的新函数,但是我找不到任何相关的文档。

  • 我已经查看了 Azure/Azure-Functions 的 GitHub 上的示例,但是缺少定时器触发器:https://github.com/Azure/Azure-Functions
  • 我还查阅了微软 Azure/Azure-Functions 页面上的自定义处理程序文档,但只有针对 HTTP 触发器的内容:https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-other?tabs=go%2Cwindows

不清楚如何在 main.go 中执行函数,以按照 function.json 中配置的 cron 调度执行。
目的是每小时执行一次函数。这是我正在使用的 /functionname/function.json 文件内容:

{
"bindings": [
{
"name": "timer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 0 * * * *"
}
]
}

英文:

I'm trying to configure a new function in my Golang custom handler that uses a timer trigger. But I haven't been able to find any documentation for it.

It's unclear how the function is executed in main.go on the cron schedule configured in function.json.
The intent is to execute the function once an hour. This is the /functionname/function.json file I'm using:

{
  "bindings": [
    {
      "name": "timer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 * * * *"
    }
  ]
}

答案1

得分: 1

在配置定时触发器(timer trigger)和HTTP触发器(HTTP trigger)时,有一些区别。

以下是我在尝试配置定时触发器函数时注意到的区别:

  1. 在配置定时触发器时,/local.settings.json 文件需要一个名为 "AzureWebJobsStorage" 的字段,而在配置HTTP触发器时则不需要。如果没有这个字段,启动函数应用程序时会导致失败。该字段存储与函数应用程序一起使用的存储账户的连接字符串。(显然,将此文件添加到 .gitignore 中)
  2. 当函数应用程序尝试调用定时触发的函数时,它期望函数位于端点 /functionName。这与HTTP触发器不同,后者在 /api/functionName 处执行函数处理程序。

注意:定时触发器函数似乎无法设置出站的HTTP绑定。因此,即使函数设置了响应,它也不会被使用,也不会通过函数主机发送回去。目前尚不清楚为什么定时触发器会忽略出站的HTTP绑定,也不清楚如何在不使用出站绑定的情况下设置响应。

英文:

There are a few differences between configuring a custom handler with a timer trigger and an HTTP trigger.

These are the differences that I noticed while figuring out how to get the timer trigger function up and running:

  1. The /local.settings.json file requires a field called "AzureWebJobsStorage" when configuring a timer trigger, it's not required for the HTTP trigger. If not present it will cause the function app to fail when starting. This field stores the connection string for the storage account being used with the function app. (obviously add this file to .gitignore)
  2. When the function app attempts to call the timer triggered function, it expects it to be at the endpoint /functionName. This is different from the HTTP trigger which executes function handlers at /api/functionName.

Note: Timer trigger functions don't appear to be able to set an outbound http binding. So even though the function sets a response, it isn't used and does not get sent back to through the function host. It's unclear why timer triggers ignore the outbound http binding or how to set a response without using one.

huangapple
  • 本文由 发表于 2022年5月6日 07:48:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/72134721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定