简体   繁体   English

如何在 Azure EventGrid 中的一批事件中触发重试/死信特定事件

[英]How to trigger retry/deadletter specific events in a batch of events in Azure EventGrid

I've got an Azure app service with a REST API controller that can be called from Azure Event Grid. I've got an Azure app service with a REST API controller that can be called from Azure Event Grid. As you may know, Event Grid can send multiple events in a batch.您可能知道,事件网格可以批量发送多个事件。 I don't know under which circumstances this actually happens, because so far the event payload has always been an array consisting of only one single element, but the documentation makes it clear that there can be multiple events in one go.我不知道这实际上是在什么情况下发生的,因为到目前为止,事件有效负载一直是一个仅包含一个元素的数组,但文档清楚地表明在一个 go 中可以有多个事件。

Let's say I receive five events, and event #3 is somehow incorrect.假设我收到五个事件,而事件 #3 不知何故不正确。 How do I let Event Grid know that I've accepted four of the five events, and that it should retry or deadletter the third event?我如何让事件网格知道我已经接受了五个事件中的四个,并且它应该重试或死信第三个事件?

[Route("api/[controller]")]
[ApiController]
public class EventGridController : ControllerBase
{
    [HttpPost("CustomerUpdated")]
    public async Task<IActionResult> CustomerUpdated()
    {
        // ... manage subscription validation here
        
        using var reader = new StreamReader(Request.Body, Encoding.UTF8);
        var eventStr = await reader.ReadToEndAsync();
        var gridEvents = JsonConvert.DeserializeObject<IEnumerable<GridEvent>>(eventStr);
        
        // ... let's say one of five received events is somehow incorrect here
        // ... how do I tell event grid which events failed and which events were accepted
    }

Currently, I mark the entire batch as failed by returning BadRequest() even if some of the events actually succeeded.目前,即使某些事件实际上成功了,我也会通过返回BadRequest()将整个批次标记为失败。 It's an okayish "better safe than sorry" trade-off solution because I'm currently the lone ranger on this project and can as such make sure that my code is idempotent.这是一个不错的“比抱歉更安全”的权衡解决方案,因为我目前是这个项目的独行侠,因此可以确保我的代码是幂等的。 But I want to make sure that some other developer in the future can't make the codebase non-idempotent and have data inconsistencies popping up all over the place because my code tells EventGrid that the entire batch was failed even though some of the events were actually processed successfully.但是我想确保将来的其他一些开发人员不能使代码库非幂等并且到处出现数据不一致的情况,因为我的代码告诉 EventGrid 整个批次都失败了,即使某些事件是实际处理成功。 What can I do?我能做些什么?

The Azure Event Grid output batching policy is designed as All or None , see more details here . Azure 事件网格 output 批处理策略设计为All or None ,请在此处查看更多详细信息。

In the case like you described, the subscriber needs to handle a logic for skipping already successful processed events and the return status code should be for retrying delivery (eg 503).在您描述的情况下,订阅者需要处理跳过已经成功处理的事件的逻辑,并且返回状态代码应该用于重试传递(例如 503)。 Note, that the 400 Bad Request will immediately process of the deadlettering if it is enabled.请注意,如果启用了400 Bad Request ,它将立即处理死信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM