Skip to content

Commit

Permalink
implement #36
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwitt committed Jul 31, 2018
1 parent 17626d3 commit 40c2d15
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ Options
-f, --fifo Create new queues as FIFOs
-g, --group-id string FIFO Group ID to use for all messages enqueued in current command. Defaults to an string unique to this invocation.
--group-id-per-message Use a unique Group ID for every message, even messages in the same batch.
-d, --delay number Delays delivery of each message by the given number of seconds (up to 900 seconds, or 15 minutes). Defaults to immediate delivery (no delay).
--prefix string Prefix to place at the front of each SQS queue name [default: qdone_]
--fail-suffix string Suffix to append to each queue to generate fail queue name [default: _failed]
--region string AWS region for Queues [default: us-east-1]
Expand Down
3 changes: 2 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function setupVerbose (options) {
const enqueueOptionDefinitions = [
{ name: 'fifo', alias: 'f', type: Boolean, description: 'Create new queues as FIFOs' },
{ name: 'group-id', alias: 'g', type: String, defaultValue: uuid.v1(), description: 'FIFO Group ID to use for all messages enqueued in current command. Defaults to an string unique to this invocation.' },
{ name: 'group-id-per-message', type: Boolean, description: 'Use a unique Group ID for every message, even messages in the same batch.' }
{ name: 'group-id-per-message', type: Boolean, description: 'Use a unique Group ID for every message, even messages in the same batch.' },
{ name: 'delay', alias: 'd', type: Number, defaultValue: 0, description: 'Delays delivery of each message by the given number of seconds (up to 900 seconds, or 15 minutes). Defaults to immediate delivery (no delay).' }
]

exports.enqueue = function enqueue (argv) {
Expand Down
14 changes: 10 additions & 4 deletions src/enqueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function formatMessage (command, id) {
function sendMessage (qrl, command, options) {
debug('sendMessage(', qrl, command, ')')
const message = Object.assign({QueueUrl: qrl}, formatMessage(command))
// Add in delay if we're using it
if (options.delay) message.DelaySeconds = options.delay
// Add in group id if we're using fifo
if (options.fifo) {
message.MessageGroupId = options['group-id']
Expand All @@ -97,10 +99,14 @@ function sendMessageBatch (qrl, messages, options) {
// Add in group id if we're using fifo
if (options.fifo) {
params.Entries = params.Entries.map(
message => Object.assign({
MessageGroupId: options['group-id-per-message'] ? uuid.v1() : options['group-id'],
MessageDeduplicationId: uuid.v1()
}, message)
message => Object.assign(
{
MessageGroupId: options['group-id-per-message'] ? uuid.v1() : options['group-id'],
MessageDeduplicationId: uuid.v1()
},
options.delay ? {DelaySeconds: options.delay} : {},
message
)
)
}
const sqs = new AWS.SQS()
Expand Down
55 changes: 55 additions & 0 deletions test/test.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ describe('cli', function () {
}))
})

describe('qdone enqueue --delay 600 testQueue true # (queue exists, valid delay)', function () {
before(function () {
AWS.mock('SQS', 'getQueueUrl', function (params, callback) {
callback(null, {QueueUrl: `https://q.amazonaws.com/123456789101/${params.QueueName}`})
})
AWS.mock('SQS', 'sendMessage', function (params, callback) {
callback(null, {
MD5OfMessageAttributes: '00484c68...59e48f06',
MD5OfMessageBody: '51b0a325...39163aa0',
MessageId: 'da68f62c-0c07-4bee-bf5f-7e856EXAMPLE'
})
})
})
it('should have no output and exit 0',
cliTest(['enqueue', '--delay', '600', '--quiet', 'testQueue', 'true'], function (result, stdout, stderr) {
expect(stderr).to.equal('')
expect(stdout).to.equal('')
}))
})

describe('qdone enqueue testQueue true # (queue does not exist)', function () {
before(function () {
AWS.mock('SQS', 'getQueueUrl', function (params, callback) {
Expand Down Expand Up @@ -464,6 +484,41 @@ describe('cli', function () {
}))
})

describe('qdone enqueue-batch --delay 600 --fifo --group-id-per-message test/fixtures/test-unique01-x24.batch # (queue exists, group ids should be unique, message delay)', function () {
let groupIds
before(function () {
groupIds = {}
AWS.mock('SQS', 'getQueueUrl', function (params, callback) {
callback(null, {QueueUrl: `https://q.amazonaws.com/123456789101/${params.QueueName}`})
})
var messageId = 0
AWS.mock('SQS', 'sendMessageBatch', function (params, callback) {
params.Entries.forEach(message => {
groupIds[message.MessageGroupId] = true
})
callback(null, {
Failed: [],
Successful: params.Entries.map(message => ({
MD5OfMessageAttributes: '00484c68...59e48f06',
MD5OfMessageBody: '51b0a325...39163aa0',
MessageId: 'da68f62c-0c07-4bee-bf5f-56EXAMPLE-' + messageId++
}))
})
})
})
it('should print id of enqueued messages, use 3 requests, use unique group ids for every message, print total count and exit 0',
cliTest(['enqueue-batch', '--delay', '600', '--fifo', '--group-id-per-message', 'test/fixtures/test-unique01-x24.batch'], function (result, stdout, stderr) {
for (var messageId = 0; messageId < 24; messageId++) {
expect(stderr).to.contain('Enqueued job da68f62c-0c07-4bee-bf5f-56EXAMPLE-' + messageId)
}
expect(Object.keys(groupIds).length).to.equal(24)
expect(stderr).to.contain('Enqueued 24 jobs')
expect(stderr).to.contain('request 1')
expect(stderr).to.contain('request 2')
expect(stderr).to.contain('request 3')
}))
})

describe('qdone enqueue-batch test/fixtures/test-unique01-x24.batch # (queue exists, some failures)', function () {
before(function () {
AWS.mock('SQS', 'getQueueUrl', function (params, callback) {
Expand Down

0 comments on commit 40c2d15

Please sign in to comment.