Skip to content

Commit 8286abb

Browse files
committed
fix(webhook-engine): mark orphaned deliveries failed and return the friendly id on duplicates
If enqueuing the routing job throws after the delivery row is created, the row is now marked FAILED instead of being left PENDING with nothing to process it. And a duplicate ingest now responds with the delivery's friendly id (whd_...), matching a first delivery, rather than the internal row id.
1 parent 3501ec0 commit 8286abb

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

internal-packages/webhook-engine/src/engine/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,14 @@ export class WebhookEngine {
234234

235235
const claimed = await this.frontGate.set(
236236
gateKey,
237-
id,
237+
friendlyId,
238238
"EX",
239239
this.#frontGateClaimTtlSeconds(),
240240
"NX"
241241
);
242242
if (claimed !== "OK") {
243243
const existing = await this.frontGate.get(gateKey);
244-
return { outcome: "duplicate", deliveryId: existing ?? id };
244+
return { outcome: "duplicate", deliveryId: existing ?? friendlyId };
245245
}
246246

247247
const { filtered, reason } = this.#evaluateFilter(
@@ -256,6 +256,7 @@ export class WebhookEngine {
256256
([key, value]) => key.toLowerCase() === "x-trigger-test" && Boolean(value)
257257
);
258258

259+
let rowCreated = false;
259260
try {
260261
await this.prisma.webhookDelivery.create({
261262
data: {
@@ -278,6 +279,7 @@ export class WebhookEngine {
278279
filterReason: reason,
279280
},
280281
});
282+
rowCreated = true;
281283

282284
if (filtered) {
283285
this.deliveryFilteredCounter.add(1);
@@ -291,11 +293,19 @@ export class WebhookEngine {
291293
}
292294
} catch (error) {
293295
await this.frontGate.del(gateKey).catch(() => {});
296+
if (rowCreated) {
297+
await this.prisma.webhookDelivery
298+
.update({
299+
where: { id_createdAt: { id, createdAt } },
300+
data: { status: "FAILED", errorMessage: String(error), processedAt: new Date() },
301+
})
302+
.catch(() => {});
303+
}
294304
return { outcome: "enqueue_failed", error: String(error) };
295305
}
296306

297307
await this.frontGate
298-
.set(gateKey, id, "EX", this.#frontGateTtlSeconds(artifact))
308+
.set(gateKey, friendlyId, "EX", this.#frontGateTtlSeconds(artifact))
299309
.catch(() => {});
300310

301311
return { outcome: "accepted", deliveryId: id, deliveryFriendlyId: friendlyId };

internal-packages/webhook-engine/src/engine/ingest.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ containerTestWithIsolatedRedisNoClickhouse(
372372
typeof a,
373373
{ outcome: "duplicate" }
374374
>;
375-
expect(duplicate.deliveryId).toBe(accepted.deliveryId);
375+
expect(duplicate.deliveryId).toBe(accepted.deliveryFriendlyId);
376376

377377
const rows = await prisma.webhookDelivery.findMany({
378378
where: { webhookEndpointId: endpoint.id },

0 commit comments

Comments
 (0)