Skip to content

Commit

Permalink
Add copy option - messages are not deleted from source queue - resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Jones committed Oct 13, 2021
1 parent f73a8c6 commit f11782c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 deletions.
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ const toQuestion = {

const handleAction = (from, to, options) => {
const questions = [];
let copy = false;
let copiedOrMoved = 'moved';

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

if (options.copy) {
copy = true;
copiedOrMoved = 'copied';
}

if (!from) {
questions.push(fromQuestion);
}
Expand All @@ -57,6 +64,7 @@ const handleAction = (from, to, options) => {
count = await handle({
sourceQueueUrl,
targetQueueUrl,
copy,
sqs,
prompt,
skipPrompt: options.yes,
Expand All @@ -66,13 +74,14 @@ const handleAction = (from, to, options) => {
process.exit(1);
}

console.log(`${count} messages moved from ${sourceQueueUrl} to ${targetQueueUrl}!`);
console.log(`${count} messages ${copiedOrMoved} from ${sourceQueueUrl} to ${targetQueueUrl}!`);
});
};

program
.arguments('[from] [to]')
.option('-r, --region [value]', 'The AWS region')
.option('-y, --yes', 'Non interactive message moving')
.option('-c, --copy', 'Copy messages to new queue, do not delete')
.action(handleAction)
.parse(process.argv);
10 changes: 8 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Spinner } = require('clui');
const handle = async ({
sourceQueueUrl,
targetQueueUrl,
copy,
sqs,
prompt,
skipPrompt,
Expand All @@ -14,12 +15,17 @@ const handle = async ({
throw new Error(`The queue ${sourceQueueUrl} is empty!`);
}

let copyOrMove = 'move';
if (copy) {
copyOrMove = 'copy';
}

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

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

await Promise.all(promises).then(() => {
Expand Down
56 changes: 46 additions & 10 deletions src/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,49 @@
const { handle } = require('./main');

describe('handle', () => {
const sourceQueueUrl = 'https://sqs.region.amazonaws.com/123456789/srcQueue';
const targetQueueUrl = 'https://sqs.region.amazonaws.com/123456789/targetQueue';

test('to move messages', async () => {
const sqs = {
getCount: jest.fn(() => 3),
moveMessage: jest.fn(),
};

const prompt = jest.fn(() => ({ move: true }));
const copy = false;

await handle({
sourceQueueUrl: sourceQueueUrl,
targetQueueUrl: targetQueueUrl,
copy,
sqs,
prompt,
});

expect(sqs.getCount).toHaveBeenCalled();
expect(sqs.moveMessage).toHaveBeenCalledWith(sourceQueueUrl, targetQueueUrl, copy);
});

test('to copy messages', async () => {
const sqs = {
getCount: jest.fn(() => 3),
moveMessage: jest.fn(),
};

const prompt = jest.fn(() => ({ move: true }));
const copy = true;

await handle({
sourceQueueUrl: 'https://sqs.region.amazonaws.com/123456789/srcQueue',
targetQueueUrl: 'https://sqs.region.amazonaws.com/123456789/targetQueue',
sourceQueueUrl,
targetQueueUrl,
copy,
sqs,
prompt,
});

expect(sqs.getCount).toHaveBeenCalled();
expect(sqs.moveMessage).toHaveBeenCalledWith(sourceQueueUrl, targetQueueUrl, copy);
});

test('to move messages without prompt', async () => {
Expand All @@ -30,16 +57,19 @@ describe('handle', () => {
};

const prompt = jest.fn();
const copy = false;

await handle({
sourceQueueUrl: 'https://sqs.region.amazonaws.com/123456789/srcQueue',
targetQueueUrl: 'https://sqs.region.amazonaws.com/123456789/targetQueue',
sourceQueueUrl,
targetQueueUrl,
copy,
sqs,
prompt,
skipPrompt: true,
});

expect(sqs.getCount).toHaveBeenCalled();
expect(sqs.moveMessage).toHaveBeenCalledWith(sourceQueueUrl, targetQueueUrl, copy);
});

describe('reject promise', () => {
Expand All @@ -50,10 +80,12 @@ describe('handle', () => {
};

const prompt = jest.fn(() => ({ move: true }));
const copy = false;

expect(handle({
sourceQueueUrl: 'https://sqs.region.amazonaws.com/123456789/srcQueue',
targetQueueUrl: 'https://sqs.region.amazonaws.com/123456789/targetQueue',
sourceQueueUrl,
targetQueueUrl,
copy,
sqs,
prompt,
})).rejects.toEqual({ message: 'getCount' });
Expand All @@ -66,10 +98,12 @@ describe('handle', () => {
};

const prompt = jest.fn(() => ({ move: true }));
const copy = false;

expect(handle({
sourceQueueUrl: 'https://sqs.region.amazonaws.com/123456789/srcQueue',
targetQueueUrl: 'https://sqs.region.amazonaws.com/123456789/targetQueue',
sourceQueueUrl,
targetQueueUrl,
copy,
sqs,
prompt,
})).rejects.toEqual(new Error('moveMessage'));
Expand All @@ -82,10 +116,12 @@ describe('handle', () => {
};

const prompt = jest.fn(() => ({ move: true }));
const copy = false;

expect(handle({
sourceQueueUrl: 'https://sqs.region.amazonaws.com/123456789/srcQueue',
targetQueueUrl: 'https://sqs.region.amazonaws.com/123456789/targetQueue',
sourceQueueUrl,
targetQueueUrl,
copy,
sqs,
prompt,
})).rejects.toEqual(new Error('The queue https://sqs.region.amazonaws.com/123456789/srcQueue is empty!'));
Expand Down
6 changes: 4 additions & 2 deletions src/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const createClient = (sqs) => {
);
});

const moveMessage = (sourceQueueUrl, targetQueueUrl) => (
const moveMessage = (sourceQueueUrl, targetQueueUrl, copy) => (
new Promise(async (resolve, reject) => {
try {
const receivedMessage = await receiveMessage(sourceQueueUrl);
Expand All @@ -48,7 +48,9 @@ const createClient = (sqs) => {
const { Body, ReceiptHandle } = receivedMessage;

await sendMessage(targetQueueUrl, Body);
await deleteMessage(sourceQueueUrl, ReceiptHandle);
if (!copy) {
await deleteMessage(sourceQueueUrl, ReceiptHandle);
}

resolve(ReceiptHandle);
} catch (error) {
Expand Down
26 changes: 26 additions & 0 deletions src/sqs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ describe('moveMessage', () => {
const handle = await sqs.moveMessage(
'https://sqs.region.amazonaws.com/123456789/srcQueue',
'https://sqs.region.amazonaws.com/123456789/targetQueue',
false,
);
expect(handle).toEqual('ReceiptHandle');
});

test('to resolve promise - copy', async () => {
const sqs = createClient({
receiveMessage: mockCallbackFunction(null, {
Messages: [
{
Body: 'Body',
ReceiptHandle: 'ReceiptHandle',
},
],
}),
sendMessage: mockCallbackFunction(null, {}),
});

const handle = await sqs.moveMessage(
'https://sqs.region.amazonaws.com/123456789/srcQueue',
'https://sqs.region.amazonaws.com/123456789/targetQueue',
true,
);
expect(handle).toEqual('ReceiptHandle');
});
Expand All @@ -87,6 +109,7 @@ describe('moveMessage', () => {
expect(sqs.moveMessage(
'https://sqs.region.amazonaws.com/123456789/srcQueue',
'https://sqs.region.amazonaws.com/123456789/targetQueue',
false,
)).rejects.toEqual({ message: 'error' });
});

Expand All @@ -110,6 +133,7 @@ describe('moveMessage', () => {
await sqs.moveMessage(
'https://sqs.region.amazonaws.com/123456789/srcQueue',
'https://sqs.region.amazonaws.com/123456789/targetQueue',
false,
);
expect(mockSqs.receiveMessage).toHaveBeenCalledWith(
{ QueueUrl: 'https://sqs.region.amazonaws.com/123456789/srcQueue' },
Expand Down Expand Up @@ -137,6 +161,7 @@ describe('moveMessage', () => {
await sqs.moveMessage(
'https://sqs.region.amazonaws.com/123456789/srcQueue',
'https://sqs.region.amazonaws.com/123456789/targetQueue',
false,
);
expect(mockSqs.sendMessage).toHaveBeenCalledWith(
{
Expand Down Expand Up @@ -167,6 +192,7 @@ describe('moveMessage', () => {
await sqs.moveMessage(
'https://sqs.region.amazonaws.com/123456789/srcQueue',
'https://sqs.region.amazonaws.com/123456789/targetQueue',
false,
);
expect(mockSqs.deleteMessage).toHaveBeenCalledWith(
{
Expand Down

0 comments on commit f11782c

Please sign in to comment.