Skip to content

Commit d18da0c

Browse files
committed
Log message Ids when sending
1 parent 5b76e5a commit d18da0c

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

lambdas/api-handler/src/handlers/patch-letter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@ export default function createPatchLetterHandler(
6868
);
6969
}
7070

71-
await enqueueLetterUpdateRequests(
71+
const messageIds = await enqueueLetterUpdateRequests(
7272
[updateLetterCommand],
7373
commonIds.value.correlationId,
7474
deps,
7575
);
7676
deps.logger.info({
77-
description: "Enqueued letter update requests",
77+
description: "Enqueued letter update request",
7878
supplierId: commonIds.value.supplierId,
7979
letterId,
8080
correlationId: commonIds.value.correlationId,
81+
messageId: messageIds[0],
8182
});
8283

8384
return {

lambdas/api-handler/src/handlers/post-letters.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default function createPostLettersHandler(
7171
);
7272
}
7373

74-
await enqueueLetterUpdateRequests(
74+
const messageIds = await enqueueLetterUpdateRequests(
7575
mapToUpdateCommands(postLettersRequest, commonIds.value.supplierId),
7676
commonIds.value.correlationId,
7777
deps,
@@ -82,6 +82,7 @@ export default function createPostLettersHandler(
8282
supplierId: commonIds.value.supplierId,
8383
letterIds: postLettersRequest.data.map((letter) => letter.id),
8484
correlationId: commonIds.value.correlationId,
85+
messageIds,
8586
});
8687

8788
return {

lambdas/api-handler/src/services/__tests__/letter-operations.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,25 @@ describe("enqueueLetterUpdateRequests function", () => {
218218
);
219219

220220
const sqsClientSendMock = sqsClient.send as jest.Mock;
221-
sqsClientSendMock.mockResolvedValue({ Failed: [] });
222-
221+
sqsClientSendMock.mockResolvedValueOnce({
222+
Successful: [{ Id: "id1" }, { Id: "id2" }, { Id: "id3" }],
223+
Failed: [],
224+
});
225+
sqsClientSendMock.mockResolvedValueOnce({
226+
Successful: [{ Id: "id4" }, { Id: "id5" }],
227+
Failed: [],
228+
});
229+
sqsClientSendMock.mockResolvedValueOnce({
230+
Successful: [{ Id: "id6" }],
231+
Failed: [],
232+
});
223233
const result = await enqueueLetterUpdateRequests(
224234
updateLetterCommands,
225235
"correlationId1",
226236
deps,
227237
);
228238

229-
expect(result).toBeUndefined();
239+
expect(result).toEqual(["id1", "id2", "id3", "id4", "id5", "id6"]);
230240

231241
// processes 10 at a time (25 -> 10+10+5)
232242
expect(sqsClientSendMock).toHaveBeenCalledTimes(3);
@@ -277,14 +287,12 @@ describe("enqueueLetterUpdateRequests function", () => {
277287
makeUpdateLetterCommand(i),
278288
);
279289

280-
const result = await enqueueLetterUpdateRequests(
290+
await enqueueLetterUpdateRequests(
281291
updateLetterCommands,
282292
"correlationId1",
283293
deps,
284294
);
285295

286-
expect(result).toBeUndefined();
287-
288296
// 12 = 10 + 2
289297
expect(deps.sqsClient.send).toHaveBeenCalledTimes(2);
290298

lambdas/api-handler/src/services/letter-operations.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LetterBase, LetterRepository } from "@internal/datastore";
22
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
33
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
4-
import { SendMessageBatchCommand } from "@aws-sdk/client-sqs";
4+
import { MessageSystemAttributeNameForSends, SendMessageBatchCommand } from "@aws-sdk/client-sqs";
55
import NotFoundError from "../errors/not-found-error";
66
import { UpdateLetterCommand } from "../contracts/letters";
77
import { ApiErrorDetail } from "../contracts/errors";
@@ -95,18 +95,19 @@ export async function enqueueLetterUpdateRequests(
9595
updateLetterCommands: UpdateLetterCommand[],
9696
correlationId: string,
9797
deps: Deps,
98-
) {
98+
): Promise<string[]> {
9999
const BATCH_SIZE = 10; // SQS SendMessageBatch max
100100
const CONCURRENCY = 5; // number of parallel batch API calls
101101

102102
const batches = chunk(updateLetterCommands, BATCH_SIZE);
103103

104104
// send batches in groups with limited concurrency
105105
// BATCH_SIZE * CONCURRENCY is the number of total updates / db calls in-flight
106+
const messageIds: string[] = [];
106107
for (let i = 0; i < batches.length; i += CONCURRENCY) {
107108
const window = batches.slice(i, i + CONCURRENCY);
108109

109-
await Promise.all(
110+
const batchMessageIds: string[][] = await Promise.all(
110111
window.map(async (batch, batchIdx) => {
111112
const entries = batch.map((request, idx) => ({
112113
Id: `${i + batchIdx}-${idx}`, // unique per batch entry
@@ -130,14 +131,18 @@ export async function enqueueLetterUpdateRequests(
130131
correlationId,
131132
});
132133
}
134+
return (result.Successful || []).map((message) => message.Id);
133135
} catch (error) {
134136
deps.logger.error({
135137
err: error,
136138
description: "Error enqueuing letter status updates",
137139
correlationId,
138140
});
141+
return [];
139142
}
140143
}),
141144
);
145+
messageIds.push(...batchMessageIds.flat());
142146
}
147+
return messageIds;
143148
}

0 commit comments

Comments
 (0)