简体   繁体   English

处理后从SQS队列中删除消息

[英]Deleting messages from SQS queue after processing

My application sends messages to a AWS SQS queue for jobs that need some sort of background processing. 我的应用程序将消息发送到AWS SQS队列,以获取需要某种后台处理的作业。 My processing daemon receives and processes the messages like this: 我的处理守护进程接收并处理这样的消息:

$result = $sqsClient->receiveMessage([
    'QueueUrl' => \Myapp\Config::get('sqs.queue'),
    'WaitTimeSeconds' => 20,
    'MaxNumberOfMessages' => 1
]);

if (isset($result['Messages'])) {
    foreach ($result->getPath('Messages/*/Body') as $messageBody) {
        $handler = new \Myapp\Handler();
        $handler->dispatch($messageBody);
    }
}

This works fine, however I can see from the SQS console that the messages that my script retrieves are put into the "Messages in Flight" category, and then after a while they're put back into "Messages Available", and my script ends up picking them up again. 这工作正常,但我可以从SQS控制台看到我的脚本检索的消息被放入“飞行中的消息”类别,然后一段时间后它们被放回“消息可用”,我的脚本结束再次接他们。

How can I remove the messages from the SQS queue, or even better mark them as completed? 如何从SQS队列中删除消息,或者更好地将它们标记为已完成?

When you receive a message from an SQS queue, the message will (by default) return to the queue 30 seconds later. 当您从SQS队列收到消息时,该消息将(默认情况下)在30秒后返回队列。 This is to handle cases where processing of the message crashes and the message needs to be processed again. 这是为了处理消息处理崩溃并且需要再次处理消息的情况。

Once your message is successfully processed, use deleteMessage to delete the message. 成功处理消息后,使用deleteMessage删除消息。 You'll need the receiptHandle value from the message when you received it from receiveMessage in order to delete the message. 当您从receiveMessage收到消息时,您需要来自消息的receiptHandle值才能删除消息。

If typical processing of your message may take more than 30 seconds, then you can configure your queue to increase that "return to queue" time. 如果消息的典型处理可能需要30秒以上,那么您可以配置队列以增加“返回队列”时间。 This is called "Default Visibility Timeout" in the SQS queue configuration. 这在SQS队列配置中称为“默认可见性超时”。

Also be aware that Amazon SQS works in such a way that: 另请注意,Amazon SQS的工作方式如下:

  1. messages may be received out-of-order compared to how they were added to the queue 与将它们添加到队列的方式相比,可以无序地接收消息
  2. messages may be received twice, so allow your message processor to handle these cases 消息可能会被接收两次,因此请允许消息处理器处理这些情况

You do need to delete messages after you process them - that would be considered 'marking them completed'; 您需要在处理后删除消息 - 这将被视为“标记已完成”; however, if something downstream needs to act on this 'completed'' action, its not uncommon for me to post a 'completion' message to another SQS queue, and then delete the message from the incoming queue. 但是,如果下游的某些内容需要对此“已完成”的操作执行操作,那么我将“完成”消息发布到另一个SQS队列并且然后从传入队列中删除该消息并不罕见。 In this way another process, either now or down the road can take action that needs to be done when the first worker completes its work, ie chaining those processes together in a decoupled manner. 通过这种方式,现在或将来的另一个过程可以采取在第一个工人完成其工作时需要执行的操作,即以分离的方式将这些过程链接在一起。

If there is nothing downstream that needs to be done, simply deleting it is enough. 如果没有任何下游需要完成,只需删除它就足够了。

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

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