如何识别 SQS 队列正在工作或为空

发布于 2025-01-12 23:46:32 字数 1560 浏览 1 评论 0原文

我有一个 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 队列是活动的还是空的?

在本文档中 https://docs.aws .amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/confirm-queue-is-empty.html 它说:

当几分钟内全部为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2025-01-19 23:46:32

看来您的要求是:

  • 确保每条消息都按照严格的顺序处理
  • 不要向 Lambda 函数发送多条消息(因为担心超时)

我建议:

  • 使用 Amazon SQS FIFO (先进先出) ,先出)队列
  • 批量大小设置为 1,以便仅向 Lambda 函数发送一条消息
  • 对每条消息使用相同 MessageGroupId -- 这将确保 Lambda 不会处理正在处理另一条具有相同 ID 的消息时的消息

It appears that your requirement is:

  • Ensure each message is processed in strict order
  • Do not send multiple messages to the Lambda function (for fear of timeout)

I would recommend:

  • Use an Amazon SQS FIFO (First-In, First-Out) queue
  • Set the Batch Size to 1 to only send a single message to the Lambda function
  • Use the same MessageGroupId for each message -- this will ensure that Lambda does not process a message while another message with the same ID is being processed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文