-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
401 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
const sendMessageBatch = require('./sqs/send-message-batch'); | ||
const QueueProcessor = require('./sqs/queue-processor'); | ||
|
||
module.exports = ({ call, getService, logger }) => ({ | ||
sendMessageBatch: sendMessageBatch({ call, getService, logger }) | ||
}); | ||
module.exports = ({ call, getService, logger }) => { | ||
const smb = sendMessageBatch({ call, getService, logger }); | ||
return { | ||
sendMessageBatch: smb, | ||
QueueProcessor: QueueProcessor({ sendMessageBatch: smb }) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require('smart-fs'); | ||
const path = require('path'); | ||
const Joi = require('joi-strict'); | ||
const { wrap } = require('lambda-async'); | ||
|
||
module.exports = ({ sendMessageBatch }) => ({ queueUrl, stepsDir }) => { | ||
const steps = fs | ||
.readdirSync(stepsDir) | ||
.reduce((p, step) => Object.assign(p, { | ||
[step.slice(0, -3)]: (() => { | ||
const { schema, handler, next } = fs.smartRead(path.join(stepsDir, step)); | ||
return { | ||
handler: (payload, event) => { | ||
Joi.assert(payload, schema, `Invalid payload received for step: ${payload.name}`); | ||
return handler(payload, event); | ||
}, | ||
schema, | ||
next | ||
}; | ||
})() | ||
}), {}); | ||
|
||
return wrap((event) => { | ||
if (event.Records.length !== 1) { | ||
throw new Error( | ||
'Lambda SQS subscription is mis-configured! ' | ||
+ 'Please only process one event at a time for retry resilience.' | ||
); | ||
} | ||
return Promise.all(event.Records.map(async (e) => { | ||
const payload = JSON.parse(e.body); | ||
if (!(payload instanceof Object && !Array.isArray(payload))) { | ||
throw new Error(`Invalid Event Received: ${e.body}`); | ||
} | ||
const step = steps[payload.name]; | ||
if (payload.name === undefined) { | ||
throw new Error('Received step event that is missing "name" property.'); | ||
} | ||
if (step === undefined) { | ||
throw new Error(`Invalid step provided: ${payload.name}`); | ||
} | ||
const messages = await step.handler(payload, e); | ||
if (messages.length !== 0 && step.next.length === 0) { | ||
throw new Error(`No output allowed for step: ${payload.name}`); | ||
} | ||
Joi.assert( | ||
messages, | ||
Joi.array().items(step.next.map((n) => steps[n].schema)), | ||
`Unexpected/Invalid next step(s) returned for: ${payload.name}` | ||
); | ||
await sendMessageBatch(messages, queueUrl); | ||
return payload; | ||
})); | ||
}); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const expect = require('chai').expect; | ||
const { describe } = require('node-tdd'); | ||
const index = require('../../../src/index'); | ||
|
||
describe('Testing QueueProcessor', { | ||
useNock: true, | ||
record: console, | ||
envVarsFile: 'config.env.yml' | ||
}, () => { | ||
let aws; | ||
let processor; | ||
let executor; | ||
before(() => { | ||
aws = index({ logger: console }); | ||
processor = aws.sqs.QueueProcessor({ | ||
queueUrl: process.env.QUEUE_URL, | ||
stepsDir: `${__filename}_steps` | ||
}); | ||
executor = (records) => new Promise((resolve, reject) => { | ||
processor({ | ||
Records: records.map((r) => ({ body: JSON.stringify(r) })) | ||
}, {}, (err, resp) => { | ||
if (err !== null) { | ||
reject(err); | ||
} else { | ||
resolve(resp); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
it('Testing step1 -> [step2]', async () => { | ||
const result = await executor([{ name: 'step1' }]); | ||
expect(result).to.deep.equal([{ name: 'step1' }]); | ||
}); | ||
|
||
it('Testing step2 -> []', async () => { | ||
const result = await executor([{ name: 'step2' }]); | ||
expect(result).to.deep.equal([{ name: 'step2' }]); | ||
}); | ||
|
||
it('Testing bad-output', async ({ capture }) => { | ||
const result = await capture(() => executor([{ name: 'bad-output' }])); | ||
expect(result.message).to.equal( | ||
'Unexpected/Invalid next step(s) returned for: bad-output ' | ||
+ '[\n {\n "name" \u001b[31m[1]\u001b[0m: "unknown-step"\n }\n]' | ||
+ '\n\u001b[31m\n[1] "name" must be one of [step2]\u001b[0m' | ||
); | ||
}); | ||
|
||
it('Testing disallowed-output', async ({ capture }) => { | ||
const result = await capture(() => executor([{ name: 'disallowed-output' }])); | ||
expect(result.message).to.equal('No output allowed for step: disallowed-output'); | ||
}); | ||
|
||
it('Testing unknown-step', async ({ capture }) => { | ||
const result = await capture(() => executor([{ name: 'unknown-step' }])); | ||
expect(result.message).to.equal('Invalid step provided: unknown-step'); | ||
}); | ||
|
||
it('Testing unnamed-step', async ({ capture }) => { | ||
const result = await capture(() => executor([{}])); | ||
expect(result.message).to.equal('Received step event that is missing "name" property.'); | ||
}); | ||
|
||
it('Testing invalid event format', async ({ capture }) => { | ||
const result = await capture(() => executor([['element']])); | ||
expect(result.message).to.equal('Invalid Event Received: ["element"]'); | ||
}); | ||
|
||
it('Testing invalid step payload', async ({ capture }) => { | ||
const result = await capture(() => executor([{ name: 'step1', unexpected: 'value' }])); | ||
expect(result.message).to.equal( | ||
'Invalid payload received for step: step1 ' | ||
+ '{\n "name": "step1",\n "unexpected" \u001b[31m[1]\u001b[0m: "value"\n}\n\u001b[31m\n' | ||
+ '[1] "unexpected" is not allowed\u001b[0m' | ||
); | ||
}); | ||
|
||
it('Testing multiple records', async ({ capture }) => { | ||
const result = await capture(() => executor([{ name: 'step1' }, { name: 'step1' }])); | ||
expect(result.message).to.equal( | ||
'Lambda SQS subscription is mis-configured! ' | ||
+ 'Please only process one event at a time for retry resilience.' | ||
); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
.../queue-processor.spec.js__cassettes/testingQueueProcessor_testingBadOutput_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
1 change: 1 addition & 0 deletions
1
...processor.spec.js__cassettes/testingQueueProcessor_testingDisallowedOutput_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
1 change: 1 addition & 0 deletions
1
...ocessor.spec.js__cassettes/testingQueueProcessor_testingInvalidEventFormat_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
1 change: 1 addition & 0 deletions
1
...ocessor.spec.js__cassettes/testingQueueProcessor_testingInvalidStepPayload_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
1 change: 1 addition & 0 deletions
1
...-processor.spec.js__cassettes/testingQueueProcessor_testingMultipleRecords_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
10 changes: 10 additions & 0 deletions
10
...queue-processor.spec.js__cassettes/testingQueueProcessor_testingStep1Step2_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"scope": "https://sqs.us-west-2.amazonaws.com:443", | ||
"method": "POST", | ||
"path": "/", | ||
"body": "Action=SendMessageBatch&QueueUrl=https%3A%2F%2Fsqs.us-west-2.amazonaws.com%2FXXXXXXXXXXXX%2FqueueUrl&SendMessageBatchRequestEntry.1.Id=9207814704912e326ed773e40027e802bf31d830&SendMessageBatchRequestEntry.1.MessageBody=%7B%22name%22%3A%22step2%22%7D&Version=2012-11-05", | ||
"status": 200, | ||
"response": "<?xml version=\"1.0\"?><SendMessageBatchResponse xmlns=\"http://queue.amazonaws.com/doc/2012-11-05/\"><SendMessageBatchResult><SendMessageBatchResultEntry><Id>9207814704912e326ed773e40027e802bf31d830</Id><MessageId>8085f0e3-a423-447e-9c89-3463acc149ef</MessageId><MD5OfMessageBody>4e85d0a6c2a615ac701dc2f1f05f58e6</MD5OfMessageBody></SendMessageBatchResultEntry></SendMessageBatchResult><ResponseMetadata><RequestId>8d1d8e98-dc7e-5fc2-9c83-db8791125e19</RequestId></ResponseMetadata></SendMessageBatchResponse>" | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
.../sqs/queue-processor.spec.js__cassettes/testingQueueProcessor_testingStep2_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
1 change: 1 addition & 0 deletions
1
...ueue-processor.spec.js__cassettes/testingQueueProcessor_testingUnknownStep_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
1 change: 1 addition & 0 deletions
1
...ueue-processor.spec.js__cassettes/testingQueueProcessor_testingUnnamedStep_recording.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Joi = require('joi-strict'); | ||
|
||
module.exports.schema = Joi.object().keys({ | ||
name: Joi.string().valid('bad-output') | ||
}); | ||
|
||
module.exports.handler = async (payload, event) => [{ name: 'unknown-step' }]; | ||
|
||
module.exports.next = ['step2']; |
9 changes: 9 additions & 0 deletions
9
test/module/sqs/queue-processor.spec.js_steps/disallowed-output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Joi = require('joi-strict'); | ||
|
||
module.exports.schema = Joi.object().keys({ | ||
name: Joi.string().valid('disallowed-output') | ||
}); | ||
|
||
module.exports.handler = async (payload, event) => [{ name: 'step2' }]; | ||
|
||
module.exports.next = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Joi = require('joi-strict'); | ||
|
||
module.exports.schema = Joi.object().keys({ | ||
name: Joi.string().valid('step1') | ||
}); | ||
|
||
module.exports.handler = async (payload, event) => [{ name: 'step2' }]; | ||
|
||
module.exports.next = ['step2']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Joi = require('joi-strict'); | ||
|
||
module.exports.schema = Joi.object().keys({ | ||
name: Joi.string().valid('step2') | ||
}); | ||
|
||
module.exports.handler = async (payload, event) => []; | ||
|
||
module.exports.next = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.