feat(queue): async delivery via Cloudflare Queues with retry backoff and DLQ

This commit is contained in:
RhenCloud 2026-08-15 14:58:48 +08:00
parent 737dd7af98
commit 486f38365f
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
16 changed files with 702 additions and 22 deletions

View file

@ -11,6 +11,7 @@ import { cfEnv, cfWaitUntil, headersFrom } from "./cf";
import { log } from "./lib/log";
import { deliveryKey, kvIdempotencyStore } from "./lib/idempotency";
import { newCorrelationId } from "./lib/correlation";
import { enqueueWebhook, type DeliveryMessage } from "./queue/delivery";
const MAX_BODY_SIZE = 1024 * 1024;
@ -135,6 +136,24 @@ export async function processWebhook(
config.routes = config.routes.filter((r) => r.groupId === tenantId);
}
if (env.QUEUE) {
const message: DeliveryMessage = {
deliveryId: event.deliveryId ?? requestId,
groupId: tenantId,
provider: provider.id,
event: event.event,
payload: event.payload,
installationId: event.installationId,
receivedAt: Date.now(),
requestId,
};
const enqueue = enqueueWebhook(env, message).catch((err) =>
log.error({ requestId, err }, "Failed to enqueue webhook"),
);
waitUntil(enqueue);
return { status: 200, body: { ok: true, requestId } };
}
const dispatch = dispatchEvent(config, event, env, groups).catch((err) =>
log.error({ requestId, err }, "Dispatch failed"),
);