Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds configurable messagesPerIteration value #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ module.exports.handler = function(event, context, callback) {
};
```

### Create instance using options object or arguments

```js
const Q = new Lawos('https://sqs.eu-west-1.amazonaws.com …', SQS, Lambda, 10)

// ... or ...

const Q = new Lawos({
queueUrl: 'https://sqs.eu-west-1.amazonaws.com …',
sqs: SQS,
lambda: Lambda,
messagesPerIteration: 10
})

```

## License

Feel free to use the code, it's released using the [MIT license](https://github.com/sbstjn/lawos/blob/master/LICENSE.md).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lawos",
"version": "1.1.5",
"version": "1.2.5",
"description": "Wrapper for Amazon SQS Worker with AWS Lambda",
"main": "src/main.js",
"homepage": "https://github.com/sbstjn/lawos",
Expand Down
25 changes: 23 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
'use strict'

class Lawos {
constructor (queueUrl, sqs, lambda) {
constructor (queueUrlOrOptions, sqs, lambda, messagesPerIteration) {
if (!queueUrlOrOptions) {
throw new Error('No SQS Queue URL or options object')
}

this.maxMessages = 10
this.queueUrl = queueUrl

if (typeof queueUrlOrOptions === 'object') {
this.messagesPerIteration(queueUrlOrOptions.messagesPerIteration)
this.queueUrl = queueUrlOrOptions.queueUrl
this.sqs = queueUrlOrOptions.sqs
this.lambda = queueUrlOrOptions.lambda
} else {
this.messagesPerIteration(messagesPerIteration)
this.queueUrl = queueUrlOrOptions
}

this.aws = {
sqs: sqs,
lambda: lambda
Expand All @@ -24,6 +38,13 @@ class Lawos {
}
}

messagesPerIteration (numberOfMessages) {
if (numberOfMessages >= 1 && numberOfMessages <= 10) {
this.maxMessages = numberOfMessages
}
return this
}

invokeLambda (arn, data) {
return new Promise(resolve => {
this.aws.lambda.invoke(
Expand Down
20 changes: 19 additions & 1 deletion test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ it('is initialized with queue URL', () => {
expect(Q.queueUrl).toBe('http://example.com')
})

it('is initialized with object', () => {
const Q = new Lawos({
queueUrl: 'http://example.com'
})
expect(Q.queueUrl).toBe('http://example.com')
})

it('fails without options', () => {
expect(() => new Lawos()).toThrow(/options/)
})

it('fails without queue URL', () => {
expect(() => new Lawos()).toThrow('')
expect(() => new Lawos({ queueUrl: undefined })).toThrow(/missing/i)
})

it('can has default handler', () => {
Expand All @@ -21,6 +32,13 @@ it('can has default handler', () => {
])
})

it('can set messages per iteration', () => {
const Q = new Lawos('http://example.com')
expect(Q.messagesPerIteration(0).maxMessages).toEqual(10)
expect(Q.messagesPerIteration(11).maxMessages).toEqual(10)
expect(Q.messagesPerIteration(5).maxMessages).toEqual(5)
})

it('can set item handler', () => {
const Q = new Lawos('http://example.com')

Expand Down