Skip to content

Commit 3d4ae57

Browse files
committed
Introduce visibility timeout
1 parent d754a69 commit 3d4ae57

7 files changed

Lines changed: 19 additions & 118 deletions

File tree

infrastructure/terraform/components/api/README.md

Lines changed: 0 additions & 90 deletions
This file was deleted.

infrastructure/terraform/components/api/ddb_table_letter_queue.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ resource "aws_dynamodb_table" "letter_queue" {
1111
}
1212

1313
local_secondary_index {
14-
name = "queueTimestamp-index"
14+
name = "queueSortOrder-index"
1515
range_key = "queueTimestamp"
1616
projection_type = "ALL"
1717
}

infrastructure/terraform/components/api/module_lambda_update_letter_queue.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module "update_letter_queue" {
3535
log_subscription_role_arn = local.acct.log_subscription_role_arn
3636

3737
lambda_env_vars = merge(local.common_lambda_env_vars, {
38-
LETTER_QUEUE_TABLE_NAME = aws_dynamodb_table.letter_queue.name,
38+
LETTER_QUEUE_TABLE_NAME = "${local.csi}-letter-queue",
3939
LETTER_QUEUE_TTL_HOURS = 168 # 7 days
4040
})
4141
}
@@ -51,8 +51,8 @@ data "aws_iam_policy_document" "update_letter_queue_lambda" {
5151
]
5252

5353
resources = [
54-
aws_dynamodb_table.letter_queue.arn,
55-
"${aws_dynamodb_table.letter_queue.arn}/index/*"
54+
"arn:aws:dynamodb:${var.region}:${var.aws_account_id}:table/${local.csi}-letter-queue",
55+
"arn:aws:dynamodb:${var.region}:${var.aws_account_id}:table/${local.csi}-letter-queue/index/*"
5656
]
5757
}
5858

internal/datastore/src/__test__/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const createLetterQueueTableCommand = new CreateTableCommand({
129129
],
130130
LocalSecondaryIndexes: [
131131
{
132-
IndexName: "timestamp-index",
132+
IndexName: "queueSortOrder-index",
133133
KeySchema: [
134134
{ AttributeName: "supplierId", KeyType: "HASH" }, // Partition key for LSI
135135
{ AttributeName: "queueTimestamp", KeyType: "RANGE" }, // Sort key for LSI

internal/datastore/src/__test__/letter-queue-repository.test.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,16 @@ describe("LetterQueueRepository", () => {
5353
await db.container.stop();
5454
});
5555

56-
function assertTtl(ttl: number, before: number, after: number) {
57-
const expectedLower = Math.floor(
58-
before / 1000 + 60 * 60 * db.config.letterQueueTtlHours,
59-
);
60-
const expectedUpper = Math.floor(
61-
after / 1000 + 60 * 60 * db.config.lettersTtlHours,
62-
);
63-
expect(ttl).toBeGreaterThanOrEqual(expectedLower);
64-
expect(ttl).toBeLessThanOrEqual(expectedUpper);
65-
}
66-
6756
describe("putLetter", () => {
6857
it("adds a letter to the database", async () => {
69-
const before = Date.now();
58+
jest.useFakeTimers().setSystemTime(new Date("2026-03-04T13:15:45.000Z"));
7059

7160
const pendingLetter =
7261
await letterQueueRepository.putLetter(createLetter());
7362

74-
const after = Date.now();
75-
76-
const timestampInMillis = new Date(
77-
pendingLetter.queueTimestamp,
78-
).valueOf();
79-
expect(timestampInMillis).toBeGreaterThanOrEqual(before);
80-
expect(timestampInMillis).toBeLessThanOrEqual(after);
81-
assertTtl(pendingLetter.ttl, before, after);
63+
expect(pendingLetter.queueTimestamp).toBe("2026-03-04T13:15:45.000Z");
64+
expect(pendingLetter.visibilityTimeout).toBe("2026-03-04T13:15:45.000Z");
65+
expect(pendingLetter.ttl).toBe(1_772_633_745);
8266
expect(await letterExists(db, "supplier1", "letter1")).toBe(true);
8367
});
8468

internal/datastore/src/letter-queue-repository.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ export default class LetterQueueRepository {
2727
async putLetter(
2828
insertPendingLetter: InsertPendingLetter,
2929
): Promise<PendingLetter> {
30+
// needs to be an ISO timestamp as Db sorts alphabetically
31+
const now = new Date().toISOString();
32+
3033
const pendingLetter: PendingLetter = {
3134
...insertPendingLetter,
32-
// needs to be an ISO timestamp as Db sorts alphabetically
33-
queueTimestamp: new Date().toISOString(),
35+
queueTimestamp: now,
36+
visibilityTimeout: now,
3437
ttl: Math.floor(
3538
Date.now() / 1000 + 60 * 60 * this.config.letterQueueTtlHours,
3639
),

internal/datastore/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,18 @@ export const PendingLetterSchema = z.object({
8080
supplierId: idRef(SupplierSchema, "id"),
8181
letterId: idRef(LetterSchema, "id"),
8282
queueTimestamp: z.string().describe("Secondary index SK"),
83+
visibilityTimeout: z.string(),
8384
specificationId: z.string(),
8485
groupId: z.string(),
8586
ttl: z.int(),
8687
});
8788

8889
export type PendingLetter = z.infer<typeof PendingLetterSchema>;
8990

90-
export type InsertPendingLetter = Omit<PendingLetter, "ttl" | "queueTimestamp">;
91+
export type InsertPendingLetter = Omit<
92+
PendingLetter,
93+
"ttl" | "queueTimestamp" | "visibilityTimeout"
94+
>;
9195

9296
export const MISchemaBase = z.object({
9397
id: z.string(),

0 commit comments

Comments
 (0)