[英]Service Bus Dead Letter Queue Processing In Azure Functions
My environment.我的环境。
Now I have two functions with ServiceBusTrigger.现在我有两个 ServiceBusTrigger 函数。 One handle main queue message and another for handling dead letter queue message.一个处理主队列消息,另一个处理死信队列消息。
Scenario设想
The process which picks up the messages from the DLQ should not be something that has chances of failing so often.从 DLQ 中获取消息的过程不应该经常失败。 The fact that messages in the DLQ are going into loop itself suggests that something is really wrong here. DLQ 中的消息进入循环本身的事实表明这里确实有问题。
Can you elaborate what your process is doing and why is it failing?您能否详细说明您的流程在做什么以及为什么会失败? Are you triggering the same functionality from the main queue as you are from the DLQ?您是否从主队列触发了与 DLQ 相同的功能? That would not be wise.那是不明智的。 If you can look at the root cause of the failures, you will have less messages landing up in the DLQ to handle.如果您可以查看失败的根本原因,那么进入 DLQ 需要处理的消息就会减少。
Think of it this way.这样想。 Would you need the additional queue to retry and then push it to another queue?您是否需要额外的队列来重试然后将其推送到另一个队列? How many more queues would you need before you finally move it to a dead letter queue?在最终将其移动到死信队列之前,您还需要多少个队列? What would you do with the dead letter queue then?那么你会如何处理死信队列呢?
Anyhow, Service bus will not automatically move messages from the DLQ to another queue.无论如何,服务总线不会自动将消息从 DLQ 移动到另一个队列。 But you can trigger your own process to write it into another queue if you really need to.但是,如果确实需要,您可以触发自己的进程将其写入另一个队列。 Service bus triggered Azure functions is perfect to do such work.服务总线触发 Azure 功能非常适合做这样的工作。
To break that infinite loop from the DLQ, add a check for the delivery count value from the Brokered properties.要从 DLQ 中打破无限循环,请从 Brokered 属性中添加对传递计数值的检查。 If this value is greater than a certain threshold, then write the message into another queue.如果这个值大于某个阈值,则将消息写入另一个队列。 The delivery count is incremented each time the message is picked up.每次消息被拾取时,传递计数都会增加。
An example of this in .NET is shown below.下面显示了 .NET 中的一个示例。 I'm simulating a failure to illustrate.我正在模拟失败来说明。
using System;
using System.Threading.Tasks;
using Microsoft.Toolkit.Services;
public static void Run(string myQueueItem, Int32 deliveryCount, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
log.LogInformation($"Delivery count : {deliveryCount}");
if (deliveryCount < 3)
{
log.LogInformation("Simulating failure");
throw new TooManyRequestsException();
}
else
{
log.LogInformation($"Delivery count is now {deliveryCount}. Time to write this message into another queue.");
}
}
On my 3rd retry, I would like to send my message to another queue.在第 3 次重试时,我想将消息发送到另一个队列。 After sending a test message to my service bus, I get the following output.向我的服务总线发送测试消息后,我得到以下 output。
2022-02-22T03:05:35.281 [Information] Executing 'Functions.ServiceBusQueueTrigger1' (Reason='(null)', Id=eed59015-f100-42b4-b0bf-57a9a6ab226f)
2022-02-22T03:05:35.281 [Information] Trigger Details: MessageId: 86f8250e82fb42deb88e6b91244ca2b7, SequenceNumber: 3, DeliveryCount: 1, EnqueuedTimeUtc: 2022-02-22T03:05:35.2980000Z, LockedUntilUtc: 2022-02-22T03:06:05.2980000Z, SessionId: (null)
2022-02-22T03:05:35.531 [Information] C# ServiceBus queue trigger function processed message: my new message
2022-02-22T03:05:35.531 [Information] Delivery count : 1
2022-02-22T03:05:35.532 [Information] Simulating failure
2022-02-22T03:05:35.557 [Error] Executed 'Functions.ServiceBusQueueTrigger1' (Failed, Id=eed59015-f100-42b4-b0bf-57a9a6ab226f, Duration=253ms)Exception of type 'Microsoft.Toolkit.Services.TooManyRequestsException' was thrown.
2022-02-22T03:05:35.644 [Information] Executing 'Functions.ServiceBusQueueTrigger1' (Reason='(null)', Id=78d2df93-11f6-412b-9ae7-880ac3bf4213)
2022-02-22T03:05:35.645 [Information] Trigger Details: MessageId: 86f8250e82fb42deb88e6b91244ca2b7, SequenceNumber: 3, DeliveryCount: 2, EnqueuedTimeUtc: 2022-02-22T03:05:35.2980000Z, LockedUntilUtc: 2022-02-22T03:06:05.6730000Z, SessionId: (null)
2022-02-22T03:05:35.645 [Information] C# ServiceBus queue trigger function processed message: my new message
2022-02-22T03:05:35.646 [Information] Delivery count : 2
2022-02-22T03:05:35.646 [Information] Simulating failure
2022-02-22T03:05:35.674 [Error] Executed 'Functions.ServiceBusQueueTrigger1' (Failed, Id=78d2df93-11f6-412b-9ae7-880ac3bf4213, Duration=2ms)Exception of type 'Microsoft.Toolkit.Services.TooManyRequestsException' was thrown.
2022-02-22T03:05:35.782 [Information] Executing 'Functions.ServiceBusQueueTrigger1' (Reason='(null)', Id=b856141d-d891-4edb-a910-7e53b40b96a0)
2022-02-22T03:05:35.783 [Information] Trigger Details: MessageId: 86f8250e82fb42deb88e6b91244ca2b7, SequenceNumber: 3, DeliveryCount: 3, EnqueuedTimeUtc: 2022-02-22T03:05:35.2980000Z, LockedUntilUtc: 2022-02-22T03:06:05.8130000Z, SessionId: (null)
2022-02-22T03:05:35.783 [Information] C# ServiceBus queue trigger function processed message: my new message
2022-02-22T03:05:35.784 [Information] Delivery count : 3
2022-02-22T03:05:35.784 [Information] Delivery count is now 3. Time to write this message into another queue.**
2022-02-22T03:05:35.784 [Information] Executed 'Functions.ServiceBusQueueTrigger1' (Succeeded, Id=b856141d-d891-4edb-a910-7e53b40b96a0, Duration=2ms)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.