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.
Sqsumer depends on AWS SDK for JavaScript in Node.js and bluebird.
$ > npm install bluebird aws-sdk sqsumer --save
new Sqsumer(queueUrl, sqs[,
messagesPerIteration
])
- 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 (
An Sqsumer instance.
instance.item(workerCallback)
- 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.
A reference to of its instance, so calls can be chained.
instance.work(stopConditionCallback)
- 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.
A promise, which will be resolved with the worker session metrics.
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)
})
}
MIT License.
For the complete information, please refer to the license file.