Skip to content

Commit 8864faa

Browse files
ericallamTrigger.dev RepoOps
authored andcommitted
fix(webhooks): create delivery partitions from today forward
Webhook delivery partitions are now created from the current day forward. Previously, setup also created empty partitions for every past day in the retention window. Mono-RevId: d43a9430d57497bffd6cfa8f643cd60ad8b7e5e0
1 parent bd28d05 commit 8864faa

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

‎internal-packages/webhook-engine/src/engine/partitions.test.ts‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ postgresTest("ensurePartitions creates the window and is idempotent", async ({ p
6262
const now = new Date(Date.UTC(2026, 6, 15));
6363

6464
const first = await ensurePartitions(prisma, { now, lookaheadDays: 7, retentionDays: 3 });
65-
// retention(3) back .. lookahead(7) forward, inclusive of both ends = 11 day buckets.
66-
expect(first.created).toHaveLength(11);
67-
expect(await partitionExists(prisma, partitionName(floorDayUTC(now)))).toBe(true);
65+
expect(first.created).toHaveLength(8);
66+
expect(first.created[0]).toBe(partitionName(floorDayUTC(now)));
67+
expect(await partitionExists(prisma, partitionName(addDays(floorDayUTC(now), -1)))).toBe(false);
6868

6969
const second = await ensurePartitions(prisma, { now, lookaheadDays: 7, retentionDays: 3 });
7070
expect(second.created).toHaveLength(0);
71-
expect(second.existing).toHaveLength(11);
71+
expect(second.existing).toHaveLength(8);
7272
});
7373

7474
postgresTest("createPartition is created-then-exists", async ({ prisma }) => {
@@ -93,13 +93,13 @@ postgresTest(
9393
]);
9494

9595
for (const result of results) {
96-
expect(result.created.length + result.existing.length).toBe(11);
96+
expect(result.created.length + result.existing.length).toBe(8);
9797
}
9898
const claimed = results.flatMap((r) => r.created);
99-
expect(claimed).toHaveLength(11);
100-
expect(new Set(claimed).size).toBe(11);
99+
expect(claimed).toHaveLength(8);
100+
expect(new Set(claimed).size).toBe(8);
101101
expect(await partitionExists(prisma, partitionName(floorDayUTC(now)))).toBe(true);
102-
expect(await listDatedPartitions(prisma)).toHaveLength(11);
102+
expect(await listDatedPartitions(prisma)).toHaveLength(8);
103103
}
104104
);
105105

@@ -162,7 +162,8 @@ postgresTest(
162162
const opts = { now, lookaheadDays: 10, retentionDays: 3 };
163163

164164
const first = await bootstrapPartitions(webhookDb, opts);
165-
expect(first.created).toHaveLength(14);
165+
expect(first.created).toHaveLength(11);
166+
expect(first.created[0]).toBe(partitionName(now));
166167
expect(await partitionExists(webhookDb, old.name)).toBe(true);
167168
expect(await listDatedPartitions(prisma)).toEqual([]);
168169

@@ -303,11 +304,7 @@ containerTestWithIsolatedRedisNoClickhouse(
303304
{ timeout: 15_000 }
304305
)
305306
.toEqual({
306-
partitions: [
307-
partitionName(addDays(now, -1)),
308-
partitionName(now),
309-
partitionName(addDays(now, 1)),
310-
],
307+
partitions: [partitionName(now), partitionName(addDays(now, 1))],
311308
oldExists: false,
312309
});
313310
expect(await app.webhookDelivery.findFirst({ where: { id: delivery.id } })).toMatchObject({

‎internal-packages/webhook-engine/src/engine/partitions.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,17 @@ export type EnsureResult = {
192192
deferred: string[]; // children we couldn't detach/drop this run; retried next run
193193
};
194194

195-
/** Create the initial window without running retention. Safe to repeat before enabling ingress. */
195+
/**
196+
* Create today's partition and the lookahead without running retention. Safe to repeat before
197+
* enabling ingress. Never creates past days: ingest stamps `createdAt` at insert time, so it never
198+
* writes to one. Anything that backfills older rows (the dev delivery seed) creates its own.
199+
*/
196200
export async function bootstrapPartitions(
197201
prisma: WebhookDatabase,
198202
opts: EnsureOptions
199203
): Promise<Pick<EnsureResult, "created" | "existing">> {
200-
const today = floorDayUTC(opts.now);
201-
const start = addDays(today, -opts.retentionDays);
202-
const end = addDays(today, opts.lookaheadDays);
204+
const start = floorDayUTC(opts.now);
205+
const end = addDays(start, opts.lookaheadDays);
203206

204207
const result: Pick<EnsureResult, "created" | "existing"> = { created: [], existing: [] };
205208

0 commit comments

Comments
 (0)