Skip to content

trevorr/sqs-reader

Repository files navigation

sqs-reader: AWS SQS queue reader for Typescript

npm CircleCI Coverage Status

A small, simple, robust asynchronous queue reader for Amazon Simple Queue Service (SQS) written in Typescript.

Features

  • Configurable exponential delay between empty receive attempts to limit polling costs when idle
  • Can force polling to resume during idle delay
  • Supports immediate abort of receive/delay for graceful shutdown
  • Abstract logging mechanism uses console by default but can easily use frameworks like Winston

Installation

npm install sqs-reader

Usage

import { SQSReader } from 'sqs-reader';

const { AWS_SQS_QUEUE_URL } = process.env;

if (!AWS_SQS_QUEUE_URL) {
  throw new Error('AWS_SQS_QUEUE_URL is not configured');
}

const queueReader = new SQSReader(AWS_SQS_QUEUE_URL, async (message) => {
  console.log('Received message', message.MessageId);
});

let shuttingDown = false;

async function shutDown(): Promise<void> {
  if (!shuttingDown) {
    shuttingDown = true;
    console.info('Shutting down...');
    setTimeout((): void => {
      console.warn('Timed out waiting for shutdown');
      process.exit(1);
    }, 30000);
    try {
      queueReader.stop();
      await queueReader.join();
    } catch (err) {
      console.warn('Error in queue reader:', err);
    }
    process.exit(0);
  }
}

process.on('SIGINT', shutDown);
process.on('SIGTERM', shutDown);

(async function () {
  queueReader.start();
  await queueReader.join();
  shutDown();
})();

License

sqs-reader is available under the ISC license.

About

AWS SQS queue reader for Typescript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published