Azure Function Apps 中全局变量的替代方案是什么?
假设我想拥有一个timertrigger函数应用程序,该应用程序每10秒执行一次,并打印出越来越多的计数(1 ... 2 ... 3 ...), 在不使用环境变量的情况下,如何实现这一目标?
Lets say I want to have a TimerTrigger function app that executes every 10 seconds and prints an increasing count(1...2...3...),
how can I achieve this WITHOUT using environment variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经在使用Azure存储帐户来用于您的功能。在该存储帐户中创建一个表格,然后在此处增加计数器。这具有跨功能重新启动的额外好处。
由于您使用的是TimerTrigger,因此隐含的是,该函数运行的实例只有一个实例。如果不是这种情况,您可能最终处于种族状态,其中有两个或多个实例,以使您的计数器不正确地增加。
You're already using an Azure Storage account for your function. Create a table within that storage account, and increment the counter there. This has the added benefit of persisting across function restarts.
Since you're using a TimerTrigger, it's implicit that there will only ever be one instance of the function running. If this were not the case, you could end up in a race condition with two or more instances interleaving to incorrectly increment your counter.
我建议您查看耐用函数。这是 Azure Functions 的扩展,允许在(协调器)函数中添加状态。
在您的情况下,您可以使用单个 HTTP 触发的启动函数来启动长时间运行的协调器函数。 HTTP 函数将初始
count
值传递给 Orchestrator 函数。您可以使用计时器功能持久功能的 让协调器在继续/重新启动之前等待指定的时间。计时器到期后,count
值会递增,您可以通过调用ContinueAsNew
方法使用新的count
值重新启动 Orchestrator 函数。如果您需要有关耐用功能的更多详细信息,我有相当多的视频来解释这些概念。
I suggest you look into Durable Functions. This is an extension for Azure Functions that allow state in your (orchestrator) functions.
In your case, you can have a single HTTP triggered starter function that starts a long running orchestrator function. The HTTP function passes the initial
count
value to the orchestrator function. You can use the Timer functionality of Durable Functions to have the orchestrator wait for the specified amount of time before continuing/restarting. After the timer expires, thecount
value is incremented and you can restart the orchestrator function with this newcount
value by calling theContinueAsNew
method.This periodic work example is almost what you need I think. You still need to add the initial
count
to be read as the input, and increment it before theContinueAsNew
method is called.If you need more details about Durable Functions, I have quite some videos that explain the concepts.