bbc / sqs-consumer

Build Amazon Simple Queue Service (SQS) based applications without the boilerplate
https://bbc.github.io/sqs-consumer/
Other
1.74k stars 333 forks source link

fix: clear the polling timeout when stopped #344

Closed nicholasgriffintn closed 1 year ago

nicholasgriffintn commented 1 year ago

Resolves: #234

Follow up from: https://github.com/bbc/sqs-consumer/pull/298, without the abort functionality, just the timeout.

Changes

The issue

As consumer does not clear the timeout, when calling start, stop and then start:

  // eslint-disable-next-line no-console
  console.log('STARTING SQS CONSUMER');
  consumer.start();
  // eslint-disable-next-line no-console
  console.log('STOPPING SQS CONSUMER');
  consumer.stop();
  // eslint-disable-next-line no-console
  console.log('STARTING SQS CONSUMER');
  consumer.start();

Consumer effectively calls everything twice, as the timeout is still running from the original consumer instance, as shown below (two Polling for messages logs):

STARTING SQS CONSUMER
  sqs-consumer Starting consumer +0ms
  sqs-consumer Polling for messages +0ms
STOPPING SQS CONSUMER
  sqs-consumer Stopping consumer +2ms
RECEIVED SQS STOPPED:
STARTING SQS CONSUMER
  sqs-consumer Starting consumer +0ms
  sqs-consumer Polling for messages +0ms
EXPRESS APP LISTENING ON: http://localhost:3026
  sqs-consumer Received SQS response +20s
  sqs-consumer {
  sqs-consumer   '$metadata': {
  sqs-consumer     httpStatusCode: 200,
  sqs-consumer     requestId: undefined,
  sqs-consumer     extendedRequestId: undefined,
  sqs-consumer     cfId: undefined,
  sqs-consumer     attempts: 1,
  sqs-consumer     totalRetryDelay: 0
  sqs-consumer   },
  sqs-consumer   Messages: undefined
  sqs-consumer } +1ms
RECEIVED SQS EMPTY:
  sqs-consumer Received SQS response +2ms
  sqs-consumer {
  sqs-consumer   '$metadata': {
  sqs-consumer     httpStatusCode: 200,
  sqs-consumer     requestId: undefined,
  sqs-consumer     extendedRequestId: undefined,
  sqs-consumer     cfId: undefined,
  sqs-consumer     attempts: 1,
  sqs-consumer     totalRetryDelay: 0
  sqs-consumer   },
  sqs-consumer   Messages: undefined
  sqs-consumer } +0ms
RECEIVED SQS EMPTY:
  sqs-consumer Polling for messages +1ms
  sqs-consumer Polling for messages +2ms