如何识别 SQS 队列正在工作或为空
我有一个 SQS 队列,在向该队列发送消息之前,我需要确定其中是否有活动消息。我正在使用以下打字稿代码来捕获此内容。
const queueUrl = 'https://sqs.' + 'MY_QUEUE';
const sqsClient = new SQSClient({region: process.env.AWS_DEFAULT_REGION});
const getQueueAttributesCommandInput: GetQueueAttributesCommandInput = {
QueueUrl: queueUrl,
AttributeNames: ['All']
};
const getQueueAttributesCommandOutput = await sqsClient.send(new GetQueueAttributesCommand(getQueueAttributesCommandInput));
if (getQueueAttributesCommandOutput.$metadata.httpStatusCode !== 200) {
return new ErrorCustom(this.sqsFetchError, 400, {});
}
const approximateNumberOfMessages = +getQueueAttributesCommandOutput.Attributes['ApproximateNumberOfMessages'];
const approximateNumberOfMessagesNotVisible = +getQueueAttributesCommandOutput.Attributes['ApproximateNumberOfMessagesNotVisible'];
const approximateNumberOfMessagesDelayed = +getQueueAttributesCommandOutput.Attributes['ApproximateNumberOfMessagesDelayed'];
if (approximateNumberOfMessages + approximateNumberOfMessagesNotVisible + approximateNumberOfMessagesDelayed !== 0) {
return new ErrorCustom(this.sqsIsActivePleaseTryAgain, 400, {});
}
这是正确的做法吗?如果不是,我如何确定 SQS 队列是活动的还是空的?
当几分钟内全部为0时,队列为空。
我只想问一下,这些属性统计的实时性怎么样,好像10秒的空结果就够了。
干杯。
I have an SQS queue and before sending messages to this queue I need to identify that it has active messages in it. I am using the following typescript code to catch this.
const queueUrl = 'https://sqs.' + 'MY_QUEUE';
const sqsClient = new SQSClient({region: process.env.AWS_DEFAULT_REGION});
const getQueueAttributesCommandInput: GetQueueAttributesCommandInput = {
QueueUrl: queueUrl,
AttributeNames: ['All']
};
const getQueueAttributesCommandOutput = await sqsClient.send(new GetQueueAttributesCommand(getQueueAttributesCommandInput));
if (getQueueAttributesCommandOutput.$metadata.httpStatusCode !== 200) {
return new ErrorCustom(this.sqsFetchError, 400, {});
}
const approximateNumberOfMessages = +getQueueAttributesCommandOutput.Attributes['ApproximateNumberOfMessages'];
const approximateNumberOfMessagesNotVisible = +getQueueAttributesCommandOutput.Attributes['ApproximateNumberOfMessagesNotVisible'];
const approximateNumberOfMessagesDelayed = +getQueueAttributesCommandOutput.Attributes['ApproximateNumberOfMessagesDelayed'];
if (approximateNumberOfMessages + approximateNumberOfMessagesNotVisible + approximateNumberOfMessagesDelayed !== 0) {
return new ErrorCustom(this.sqsIsActivePleaseTryAgain, 400, {});
}
Is it a correct approach? If not how can I identify that if an SQS queue is active or empty?
On this document https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/confirm-queue-is-empty.html
It says that :
When all of them are 0 for several minutes, the queue is empty.
I just want to ask that these attribute statistics are how real-time are like 10 seconds of empty results is enough.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您的要求是:
我建议:
MessageGroupId
-- 这将确保 Lambda 不会处理正在处理另一条具有相同 ID 的消息时的消息It appears that your requirement is:
I would recommend:
MessageGroupId
for each message -- this will ensure that Lambda does not process a message while another message with the same ID is being processed