如何使来自 Lambda 的批处理中的特定 SQS 消息失败? [英] How do I fail a specific SQS message in a batch from a Lambda?
问题描述
我有一个带有 SQS 触发器的 Lambda.当它被击中时,来自 SQS 的一批记录进来(我认为通常一次大约 10 个).如果我从处理程序返回失败的状态代码,将重试所有 10 条消息.如果我返回成功代码,它们都会从队列中删除.如果这 10 条消息中有 1 条失败,而我只想重试那一条怎么办?
I have a Lambda with an SQS trigger. When it gets hit, a batch of records from SQS comes in (usually about 10 at a time, I think). If I return a failed status code from the handler, all 10 messages will be retried. If I return a success code, they'll all be removed from the queue. What if 1 out of those 10 messages failed and I want to retry just that one?
exports.handler = async (event) => {
for(const e of event.Records){
try {
let body = JSON.parse(e.body);
// do things
}
catch(e){
// one message failed, i want it to be retried
}
}
// returning this causes ALL messages in
// this batch to be removed from the queue
return {
statusCode: 200,
body: 'Finished.'
};
};
我是否必须手动将该消息重新添加回队列?或者我可以从我的处理程序返回一个状态,指示一条消息失败并应该重试?
Do I have to manually re-add that ones message back to the queue? Or can I return a status from my handler that indicates that one message failed and should be retried?
推荐答案
是的,您必须手动将失败的消息重新添加回队列.
Yes you have to manually re-add the failed messages back to the queue.
我建议做的是设置一个失败计数,这样如果所有消息都失败了,你可以简单地返回所有消息的失败状态,否则如果失败计数小于10 然后您可以单独将失败的消息发送回队列.
What I suggest doing is setting up a fail count, so that if all messages failed you can simply return a failed status for all messages, otherwise if the fail count is < 10 then you can individually send back the failed messages to the queue.
这篇关于如何使来自 Lambda 的批处理中的特定 SQS 消息失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!