sqs-consumer icon indicating copy to clipboard operation
sqs-consumer copied to clipboard

handleMessage does not await properly, polls for new messages before resolve/reject

Open upugo-dev opened this issue 7 years ago • 4 comments

here is my message handler:

async function waitAndMaybeReject() {
  // Wait one second
  await new Promise(r => setTimeout(r, 5000));
  // Toss a coin
  const isHeads = Boolean(Math.round(Math.random()));

  if (isHeads) return 'yay';
  throw Error('Boo!');
}

async function foo() {
  try {
    const result = await waitAndMaybeReject();
    return result;
  } catch (e) {
    return 'caught';
  }
}

const handleSQSMessage = async message => {
  try {
    console.log('handling', message.ReceiptHandle.substr(-5));
    const res = await foo();
    console.log(res, message.ReceiptHandle.substr(-5));
  } catch (err) {
    console.log(err);
  }
};

module.exports = handleSQSMessage;

and here is my index.js

const handleSQSMessage = require('./src/sqs/handleSQSMessage');
const sqsapp = Consumer.create({
  queueUrl: auditQueueURL,
  messageAttributeNames: ['All'],
  attributeNames: ['All'],
  handleMessage: handleSQSMessage,
  terminateVisibilityTimeout: true, // returns message straight away on failure
  visibilityTimeout: process.env.MSG_EXTENSION_TIME,
  handleMessageTimeout: process.env.MSG_TOTAL_TIME_LIMIT,
  batchSize: 1,
});
sqsapp.start();

With 1 message in the queue, this produces logs that look like:

recieved w7Q==
handling w7Q==
recieved 5Gw==
handling 5Gw==
recieved xwA==
handling xwA==
recieved h/A==
handling h/A==
recieved tfQ==
handling tfQ==
recieved QeA==
handling QeA==
recieved yFA==
handling yFA==
recieved E2w==
handling E2w==
recieved OOQ==
handling OOQ==
recieved aAA==
handling aAA==
recieved MDA==
handling MDA==
recieved u1Q==
handling u1Q==
recieved iWA==
handling iWA==
caught w7Q==
recieved dyQ==
handling dyQ==
yay 5Gw==
recieved U1g==
handling U1g==
caught xwA==
recieved nvQ==
handling nvQ==
yay h/A==
recieved BUw==
handling BUw==
caught tfQ==
recieved 0JA==
handling 0JA==
caught QeA==
recieved ulA==
handling ulA==

This demonstrates (from the order of the message receipt handles) that the return/resolve value of the handleMessage fn is not waited for before the sqs-app moves on to the next message

upugo-dev avatar May 20 '19 18:05 upugo-dev

also, I have an event listener on 'message_processed' which looks like it never fires?

upugo-dev avatar May 20 '19 19:05 upugo-dev

Using your handler (but my own debug messages) I can see messages being processed sequentially and an events being fired for message_processed. This was tested using v5.2.0

Note - the order of messages received from SQS is not guaranteed.

nspragg avatar May 21 '19 07:05 nspragg

I've updated to 5.2.0 and still not getting the expected behavior (receive message A, start handling message A, wait, processed message A, receive message B, start handling message B, wait, processed message B etc...)

Is there maybe something with my settings, either sqs-consumer settings or on AWS/SQS that could be causing this?

Would you mind posting your messageHandler function and Consumer.create() app.start()?

Edit:

Note - the order of messages received from SQS is not guaranteed.

I know. I'm not concerned about the order that the messages are received in, but what I am talking about is the guarantee that "By default messages are processed one at a time – a new message won't be received until the first one has been processed. To process messages in parallel, use the batchSize option detailed below." I seem to be unable to produce this behavior.

upugo-dev avatar May 21 '19 08:05 upugo-dev

Ooops, looks like I was setting my handleMessageTimeout: 300ms rather than 300s (=30000ms), a classic units error! So the messages were actually timing out before the async call could resolve, and returning to the queue. The async calls would then eventually begin to resolve in the background, but their completion would not count as processing the message as they were in a sort of detached state from their original message.

upugo-dev avatar May 21 '19 08:05 upugo-dev