为同一服务总线队列消息多次运行的 Azure 函数

Azure function running multiple times for the same service bus queue message(为同一服务总线队列消息多次运行的 Azure 函数)

本文介绍了为同一服务总线队列消息多次运行的 Azure 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Azure 函数(基于新的 C# 函数而不是旧的 .csx 函数),每当消息进入 Azure 服务总线队列时就会触发该函数.一旦函数被触发,它就会开始处理服务总线消息.它解码消息,读取一堆数据库,更新一堆其他的,等等......这有时可能需要 30 分钟以上.

I have an Azure function (based on the new C# functions instead of the old .csx functions) that is triggered whenever a message comes into an Azure Service Bus Queue. Once the function is triggered, it starts processing the service bus message. It decodes the message, reads a bunch of databases, updates a bunch of others, etc... This can take upwards of 30 minutes at times.

由于这不是一个时间敏感的过程,30 分钟甚至 60 分钟都不是问题.问题在于,与此同时,Azure Function 似乎再次启动并一次又一次地接收相同的消息并重新处理它.这是一个问题,会导致我们的业务逻辑出现问题.

Since, this is not a time sensitive process, 30 minutes or even 60 minutes is not an issue. The problem is that in the meanwhile, Azure Function seems to kick in again and picks up the same message again and again and reprocesses it. This is an issue and cause problems in our business logic.

那么,问题是,我们能否强制 Azure 函数以单例模式运行?或者如果这不可能,我们如何更改轮询间隔?

So, the question is, can we force the Azure function to run in a singleton mode? Or if that's not possible, how do we change the polling interval?

推荐答案

AutoRenewTimeout 没有建议的那么好.它有一个缺点,您需要注意.不是保证操作.作为客户端发起的操作,它可能并且有时失败,使您处于与今天相同的状态.

AutoRenewTimeout is not as great as suggested. It has a downside that you need to be aware of. It's not a guaranteed operation. Being a client side initiated operation, it can and sometimes will fail, leaving you in the same state as you are today.

您可以做的就是检查您的设计.如果你有一个长时间运行的进程,那么你处理消息,然后将处理交给运行时间比 MaxLockDuration 更长的东西.您的功能需要很长时间的事实表明您有一个长时间运行的过程.消息传递不是为此而设计的.

What you could do to address it is to review your design. If you have a long running process, then you process the message, and hand off processing to something that can run longer than MaxLockDuration. The fact that your function is taking so long, indicates you have a long running process. Messaging is not designed for that.

一种可能的解决方案是获取消息,在存储表中注册处理意图.有另一个存储表触发功能来启动可能需要 X 分钟的处理.将其标记为单例.通过这样做,您将并行处理消息,将长时间运行处理请求"写入存储表,完成服务总线消息传递,因此不会触发它们的重新处理.通过长时间运行的处理,您可以决定如何处理失败的情况.

One of the potential solutions would be to get a message, register processing intent in a storage table. Have another storage table triggered function to kick-off processing that could take X minutes. Mark it as a singleton. By doing so, you'll be processing your messages in parallel, writing "request for long running processing" into storage table, completing Service Bus messaging and therefore not triggering their re-processing. With the long running processing you can decide how to handle failure cases.

希望对您有所帮助.

这篇关于为同一服务总线队列消息多次运行的 Azure 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为同一服务总线队列消息多次运行的 Azure 函数

基础教程推荐