检查SQS队列是否为空的有效方法 [英] Efficient way to check whether SQS queue is empty

查看:165
本文介绍了检查SQS队列是否为空的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQS Queue,多个主机从中读取消息.处理完队列中的所有消息后,我想运行一些作业(业务逻辑).

I have a SQS Queue from which messages are read by multiple hosts. I want to run some job (business logic) after all the messages in the queue have been processed.

如何检查队列是否为空?

How can I check that the queue is empty?

是的,我可以检查ApproximateNumberOfMessagesApproximateNumberOfMessagesNotVisible队列属性,但这是近似数字.当没有剩余消息时,我想停止主机轮询队列中的消息,然后运行所需的作业.

Yes, I can check for ApproximateNumberOfMessages and ApproximateNumberOfMessagesNotVisible queue attributes but these are approximate numbers. I want to stop my hosts polling for messages in the queue when there are no messages left and then run the required job.

有什么想法吗?谢谢

推荐答案

在轮询时,您可以简单地注意到API响应中的空接收.关于CloudWatch,还有另一个指标可以更好地满足此要求.从文档:

You could simply note empty receives from the API response while you're polling. Concerning CloudWatch, there is another metric that would be a better fit for this. From the documentation:

NumberOfEmptyReceives

NumberOfEmptyReceives

未返回消息的ReceiveMessage API调用的数量.

The number of ReceiveMessage API calls that did not return a message.

单位:计数

有效统计信息:平均值,最小值,最大值,总和,数据样本 (在Amazon SQS控制台中显示为样本计数")

Valid Statistics: Average, Minimum, Maximum, Sum, Data Samples (displays as Sample Count in the Amazon SQS console)

一些其他信息:

  • 该指标仅每5分钟填充一次.如果您根据此指标设置警报,则表示您的最短时间段应为5分钟.
  • 总和是您使用案例中最明智的统计信息.如果NumberOfEmptyReceives> 0,则您的轮询作业已检查队列,并且未收到任何消息.
  • This metric only gets populated every 5 minutes. If you set up an alarm based on this metric, this means your minimum period should be 5 minutes.
  • Sum is the most sensible statistic for your use case. If NumberOfEmptyReceives > 0, your polling job checked the queue and received no messages.

我个人使用此指标来设置一个cloudwatch警报,该警报将在NumberOfEmptyReceives的总和> 0后连续几个周期缩小承载我的轮询工作的自动伸缩组.我喜欢连续进行,因为这样可以更明显地看出队列不仅是空的,而且一直都空着.

I personally used this metric to set up a cloudwatch alarm that will scale down an autoscaling group that hosts my polling job after the sum of NumberOfEmptyReceives > 0 for several consecutive periods. I like doing consecutive periods because it makes it more evident that the queue was not only empty, but has stayed empty.

这篇关于检查SQS队列是否为空的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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