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.
CodePudding user response:
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