Skip to content

Commit

Permalink
Add option to specify max number of messages to move: resolves jobloc…
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Jones committed Oct 13, 2021
1 parent f73a8c6 commit 5305223
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $ aws-move-queue-messages
With all optional CLI arguments:

```sh
$ aws-move-queue-messages <from-queue-url> <to-queue-url> -r [AWS-REGION] -y
$ aws-move-queue-messages <from-queue-url> <to-queue-url> -r [AWS-REGION] -m 100 -y
```

Any CLI argument or option you do not specify will fallback to a CLI prompt. The `-y` CLI option will answer the confirmation prompt automatically with "yes".
Expand Down
10 changes: 10 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ const validateUrl = (input) => {
return valid;
};

const validateMaxMessages = (input) => {
let valid = true;
if (!input.match(/^[1-9][0-9]*$/)) {
valid = 'Please enter a valid max number of messages greater than 0!'
}

return valid;
}

module.exports = {
validateUrl,
validateMaxMessages
};
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const aws = require('aws-sdk');
const program = require('commander');
const { prompt } = require('inquirer');
const { validateUrl } = require('./helper');
const { validateMaxMessages, validateUrl } = require('./helper');
const { handle } = require('./main');
const { createClient } = require('./sqs');

Expand All @@ -28,13 +28,24 @@ const toQuestion = {
validate: validateUrl,
};

const maxQuestion = {
type: 'input',
name: 'maxMessages',
message: 'Enter the max number of messages:',
validate: validateMaxMessages,
}

const handleAction = (from, to, options) => {
const questions = [];

if (!options.region) {
questions.push(regionQuestion);
}

if (!options.maxMessages) {
questions.push(maxQuestion)
}

if (!from) {
questions.push(fromQuestion);
}
Expand All @@ -45,6 +56,7 @@ const handleAction = (from, to, options) => {

prompt(questions).then(async ({
awsRegion = options.region,
maxMessages = options.maxMessages,
sourceQueueUrl = from,
targetQueueUrl = to,
}) => {
Expand All @@ -55,6 +67,7 @@ const handleAction = (from, to, options) => {

try {
count = await handle({
maxMessages,
sourceQueueUrl,
targetQueueUrl,
sqs,
Expand All @@ -73,6 +86,7 @@ const handleAction = (from, to, options) => {
program
.arguments('[from] [to]')
.option('-r, --region [value]', 'The AWS region')
.option('-m, --maxMessages [value]', 'Max number of messages')
.option('-y, --yes', 'Non interactive message moving')
.action(handleAction)
.parse(process.argv);
17 changes: 12 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Spinner } = require('clui');

const handle = async ({
maxMessages,
sourceQueueUrl,
targetQueueUrl,
sqs,
Expand All @@ -10,16 +11,22 @@ const handle = async ({
const count = await sqs.getCount(sourceQueueUrl);
await sqs.getCount(targetQueueUrl);

if (count === 0) {
if (parseInt(count) === 0) {
throw new Error(`The queue ${sourceQueueUrl} is empty!`);
}

maxMessages = parseInt(maxMessages);
moveCount = count
if(count > maxMessages) {
moveCount = maxMessages
}

if (!skipPrompt) {
const { move } = await prompt([
{
type: 'confirm',
name: 'move',
message: `Do you want to move ${count} messages?`,
message: `Do you want to move ${moveCount} of ${count} messages?`,
default: false,
},
]);
Expand All @@ -29,12 +36,12 @@ const handle = async ({
}
}

const spinner = new Spinner(`Moving ${count} messages...`);
const spinner = new Spinner(`Moving ${moveCount} messages...`);
spinner.start();

const promises = [];

for (let i = 0; i < count; i += 1) {
for (let i = 0; i < moveCount; i += 1) {
promises.push(sqs.moveMessage(sourceQueueUrl, targetQueueUrl));
}

Expand All @@ -45,7 +52,7 @@ const handle = async ({
throw new Error(e.message);
});

return count;
return moveCount;
};

module.exports = {
Expand Down

0 comments on commit 5305223

Please sign in to comment.