Skip to content

Commit 4a975b0

Browse files
authored
fix(sqs): Support both standard and FIFO SQS queues (#490)
Currently, sending an SQS message to a standard (non-FIFO) queue will fail if the `messageGroupId` param is provided to the `sendMessage` function. The function should only include this param when it is sending a message to a FIFO queue. ## Related Issues - #489
1 parent 977ea90 commit 4a975b0

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/spacecat-shared-utils/src/sqs.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class SQS {
2525
this.log = log;
2626
}
2727

28+
/**
29+
* Check if the queue is a FIFO queue by examining its URL.
30+
* @param {string} queueUrl - the URL of the SQS queue
31+
* @returns {boolean} true if the queue is a FIFO queue, false otherwise
32+
*/
33+
static #isFifoQueue(queueUrl) {
34+
return hasText(queueUrl) && queueUrl.toLowerCase().endsWith('.fifo');
35+
}
36+
2837
/**
2938
* Send a message to an SQS queue. For FIFO queues, messageGroupId is required.
3039
* @param {string} queueUrl - The URL of the SQS queue.
@@ -43,8 +52,8 @@ class SQS {
4352
QueueUrl: queueUrl,
4453
};
4554

46-
if (hasText(messageGroupId)) {
47-
// MessageGroupId is required for FIFO queues
55+
// Only include MessageGroupId if the queue is a FIFO queue
56+
if (SQS.#isFifoQueue(queueUrl) && hasText(messageGroupId)) {
4857
params.MessageGroupId = messageGroupId;
4958
}
5059

packages/spacecat-shared-utils/test/sqs.test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ describe('SQS', () => {
189189
]);
190190
});
191191

192-
it('should include a MessageGroupId when provided', async () => {
192+
it('should include a MessageGroupId when the queue is a FIFO queue', async () => {
193193
const action = wrap(async (req, ctx) => {
194-
await ctx.sqs.sendMessage('queue-url', { key: 'value' }, 'job-id');
194+
await ctx.sqs.sendMessage('https://sqs.us-east-1.amazonaws.com/123456789012/fifo-queue.fifo', { key: 'value' }, 'job-id');
195195
}).with(sqsWrapper);
196196

197197
await action({}, context);
@@ -204,5 +204,37 @@ describe('SQS', () => {
204204
]);
205205
expect(firstSendArg.input.MessageGroupId).to.equal('job-id');
206206
});
207+
208+
it('should not include a MessageGroupId when the queue is standard queue', async () => {
209+
const action = wrap(async (req, ctx) => {
210+
// Note: no .fifo suffix
211+
await ctx.sqs.sendMessage('https://sqs.us-east-1.amazonaws.com/123456789012/standard-queue', { key: 'value' }, 'job-id');
212+
}).with(sqsWrapper);
213+
214+
await action({}, context);
215+
216+
const firstSendArg = sendStub.getCall(0).args[0];
217+
expect(Object.keys(firstSendArg.input)).to.deep.equal([
218+
'MessageBody',
219+
'QueueUrl',
220+
]);
221+
expect(firstSendArg.input.MessageGroupId).to.be.undefined;
222+
});
223+
224+
it('should not include a MessageGroupId when the queue URL is undefined', async () => {
225+
const action = wrap(async (req, ctx) => {
226+
// Edge case: no queue URL
227+
await ctx.sqs.sendMessage(undefined, { key: 'value' }, 'job-id');
228+
}).with(sqsWrapper);
229+
230+
await action({}, context);
231+
232+
const firstSendArg = sendStub.getCall(0).args[0];
233+
expect(Object.keys(firstSendArg.input)).to.deep.equal([
234+
'MessageBody',
235+
'QueueUrl',
236+
]);
237+
expect(firstSendArg.input.MessageGroupId).to.be.undefined;
238+
});
207239
});
208240
});

0 commit comments

Comments
 (0)