sqsumer

1.1.0 • Public • Published

Sqsumer - AWS SQS worker

NPM version digitalmaas NPM downloads standardjs

Library to process messages from an Amazon SQS queue with an AWS Lambda worker function or your favorite other JavaScript environment. Based of Sebastian Müller's work on lawos.

Install

Sqsumer depends on AWS SDK for JavaScript in Node.js and bluebird.

> npm install bluebird aws-sdk sqsumer --save

API

🔷 constructor

Syntax

new Sqsumer(queueUrl, sqs[,messagesPerIteration])

Parameters

  1. queueUrl
    • Address of the target SQS queue
  2. sqs
    • Instance of SQS client from AWS SDK
  3. messagesPerIteration
    • Maximum number of messages received per iteration ([1, 10], default 10)

Return Value

An Sqsumer instance.

🔷 item

Syntax

instance.item(workerCallback)

Parameters

  1. workerCallback
    • Function that processes each received message.
      • Parameters
        1. message
          • A message from the target queue.
      • Returns
        • If it is a common function, it will be wrapped in a promise, where a return value will resolve it and a throw will rejected it. It can also return a promise, which will work as expected. If the promise is resolved, the message will be removed from the queue; otherwise, no action will be taken.

Return Value

A reference to of its instance, so calls can be chained.

🔷 work

Syntax

instance.work(stopConditionCallback)

Parameters

  1. stopConditionCallback
    • Function that is invoked in the end of each iteration.
      • Parameters
        1. metrics
          • Latest metrics from the current worker session.
      • Returns
        • If it is a common function, it will be wrapped in a promise, where a return value will resolve it and a throw will rejected it. It can also return a promise, which will work as expected. If the promise is resolved, the return value will be evaluated: if truthy, worker session ends; if falsy, worker session continues. If the promise is rejected, the worker session ends.

Return Value

A promise, which will be resolved with the worker session metrics.

Example

Assuming you're using AWS Lambda:

const SQS = require('aws-sdk/clients/sqs') // who needs it all?
const sqs = new SQS()
 
const Sqsumer = require('sqsumer')
const actualWork = require('./actualWork')
 
module.exports.handler = (event, context, done) => {
  new Sqsumer('https://sqs.eu-west-1.amazonaws.com …', sqs)
    .item(message => actualWork(message))
    .work(() => context.getRemainingTimeInMillis() < 1000)
    .then(metrics => {
      done(null, metrics)
    })
}

License

MIT License.
For the complete information, please refer to the license file.

Package Sidebar

Install

npm i sqsumer

Weekly Downloads

8

Version

1.1.0

License

MIT

Unpacked Size

50.7 kB

Total Files

11

Last publish

Collaborators

  • digitalmaas