使用AWS SQS重试代码失败的消息 [英] Retrying messages where my code fails with AWS SQS

查看:272
本文介绍了使用AWS SQS重试代码失败的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试寻找有关使重试和错误队列在我的代码失败而不是在应用程序中进行内存重试的更多信息.

I've been trying to find out more information on having a retry and error queue where my code fails rather than having in-memory retries in my application.

我正在发送一条消息,内容如下:

I'm sending a message saying something along the lines of:

处理此任务的输出-希望您使用此流的内容更新此xml文件".

"Process the output for this task - It want's you to update this xml file with the contents of this stream".

我有将输出写入xml文件的代码,但有时会失败并需要重试,因为我的应用程序/人员的另一部分可能正在使用该文件.

I have code that writes output to an xml file but it can occasionally fail and need retried as It's possible that another part of my application/person is using the file at that point in time.

我要说的是每当输出代码失败时,重新发送SQS消息,告诉它开始输出过程/发送一个具有相同信息的新消息.因此,重试消息.另外,一旦重试并失败100次,我想将其移至错误队列.

What I'm trying to do is say "Whenever the ouput code fails, resend the SQS message that told it to start the output process/send a new one with the same info. Hence retrying a message." Also, once it retries and fails 100 times I want to move it to an error queue.

有人知道这个的任何实现吗?在尝试实施之前,我试图查看已经完成的事情.

Does anyone know of any kind of implementation of this? I'm trying to see something that's already been done before I start implementing.

推荐答案

SQS可以做您想做的一切,而无需付出很多努力:

SQS does everthing you want already, without much effort:

您的代码应将消息放入处理此任务的输出-希望您使用此流的内容更新此xml文件"的消息.

Your code should put the message into the queue that says "Process the output for this task - It want's you to update this xml file with the contents of this stream"

您的工作人员任务轮询队列并获取消息,然后开始工作.我喜欢为此使用Windows服务,但是同步作业或计划任务也可以使用.

Your worker tasks polls the queue and gets the message and begins the work. I like to use windows services for this, but a chron job or scheduled task works as well.

只有当工作程序成功完成任务时,它才会从队列中删除消息-这是工作程序作业在退出之前应该做的最后一件事.它不会在收到消息时从队列中删除该消息,只会在成功处理该消息后才将其删除.

Only if the worker completes the task successfully does it delete the message from the queue - that is the very last thing that the worker job should do before quitting. It doesn't remove the message from the queue when it gets it, it only removes the message if it succeeds in processing it.

如果工作程序未完成,因此消息仍在队列中,则可见性超时到期后,消息将自动再次位于队列中(或更准确地说,再次在队列中可见)-您不要无需将其放回队列中.

If the worker does not complete, and thus the message is still in the queue, then after the visibility timeout expires, the messages will be in the queue again (or more accurately be visible in the queue again) automatically - you don't need to put it back in the queue.

要实现尝试100次后失败",您需要设置死信队列":

To implement the 'fail after 100 tries', you'll want to setup a 'Dead Letter Queue': http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html

您告诉它,如果工作任务已请求该消息100次(可配置为1至1000次)并且未成功处理,则将消息自动移动到指定的死信队列.

You tell it that if a worker task has requested that message 100 times (configurable from 1 to 1000 times) and not successfully processed it, then automatically move the message to the specified dead-letter-queue.

再简单不过了-SQS只需单击几下鼠标即可为您完成所有工作-您要做的就是编写将原始消息放入第一个队列的代码,进行工作,然后删除如果任务成功完成,则来自队列的消息.

Couldn't be simpler - SQS does all the work for you with a few clicks of the mouse - all you need to do is write the code that puts the original message in the first queue, does the work, and deletes the message from the queue if/when the task completes successfully..

这篇关于使用AWS SQS重试代码失败的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆