sqsumer
v1.1.0
Published
Worker to consume AWS SQS messages, built with Serverless and AWS Lambda in mind
Downloads
47
Maintainers
Readme
Sqsumer - AWS SQS worker
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
- queueUrl
- Address of the target SQS queue
- sqs
- Instance of SQS client from AWS SDK
- messagesPerIteration
- Maximum number of messages received per iteration (
[1, 10]
, default10
)
- Maximum number of messages received per iteration (
Return Value
An Sqsumer instance.
🔷 item
Syntax
instance.item(workerCallback)
Parameters
- workerCallback
- Function that processes each received message.
- Parameters
- message
- A message from the target queue.
- message
- 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.
- Parameters
- Function that processes each received message.
Return Value
A reference to of its instance, so calls can be chained.
🔷 work
Syntax
instance.work(stopConditionCallback)
Parameters
- stopConditionCallback
- Function that is invoked in the end of each iteration.
- Parameters
- metrics
- Latest metrics from the current worker session.
- metrics
- 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.
- Parameters
- Function that is invoked in the end of each iteration.
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.