comment resolving

This commit is contained in:
Bilal Godil 2026-01-30 10:55:29 -08:00
parent 0c1a02e381
commit 79e330ff94
3 changed files with 32 additions and 11 deletions

View File

@ -47,6 +47,7 @@ export const GET = createSmartRouteHandler({
const startTime = performance.now();
const maxDurationMs = 3 * 60 * 1000;
const pollIntervalMs = 50;
const staleClaimIntervalMinutes = 5;
let totalRequestsProcessed = 0;
async function claimPendingRequests(): Promise<OutgoingRequest[]> {
@ -58,6 +59,7 @@ export const GET = createSmartRouteHandler({
SELECT id
FROM "OutgoingRequest"
WHERE "startedFulfillingAt" IS NULL
OR "startedFulfillingAt" < NOW() - (${staleClaimIntervalMinutes} * INTERVAL '1 minute')
ORDER BY "createdAt"
LIMIT 100
FOR UPDATE SKIP LOCKED
@ -67,11 +69,22 @@ export const GET = createSmartRouteHandler({
return rows;
});
}
async function deleteOutgoingRequest(id: string): Promise<void> {
await retryTransaction(globalPrismaClient, async (tx) => {
await tx.outgoingRequest.delete({ where: { id } });
});
}
async function releaseOutgoingRequest(id: string): Promise<void> {
await retryTransaction(globalPrismaClient, async (tx) => {
await tx.outgoingRequest.updateMany({
where: { id, startedFulfillingAt: { not: null } },
data: { startedFulfillingAt: null },
});
});
}
async function processRequests(requests: OutgoingRequest[]): Promise<number> {
let processed = 0;
for (const request of requests) {
try {
const results = await Promise.allSettled(
requests.map(async (request) => {
// Prisma JsonValue doesn't carry a precise shape for this JSON blob.
const options = request.qstashOptions as any;
const baseUrl = getEnvVariable("NEXT_PUBLIC_STACK_API_URL");
@ -94,13 +107,21 @@ export const GET = createSmartRouteHandler({
body: options.body,
});
await deleteOutgoingRequest(request.id);
}),
);
let processed = 0;
for (const [index, result] of results.entries()) {
if (result.status === "fulfilled") {
processed++;
} catch (error) {
captureError(
`poller-iteration-error`,
error,
);
continue;
}
captureError(
"poller-iteration-error",
result.reason,
);
await releaseOutgoingRequest(requests[index].id);
}
return processed;

View File

@ -329,7 +329,7 @@ describe.sequential('External DB Sync - Advanced Tests', () => {
const res = await client.query(`SELECT * FROM "users" WHERE "primary_email" = $1`, ['special@example.com']);
expect(res.rows.length).toBe(1);
expect(res.rows[0].display_name).toBe(specialName);
});
}, TEST_TIMEOUT);
/**
* What it does:

View File

@ -288,7 +288,7 @@ describe.sequential('External DB Sync - Basic Tests', () => {
`);
expect(tableCheck.rows[0].exists).toBe(true);
await verifyInExternalDb(client, 'auto-create@example.com', 'Auto Create User');
});
}, TEST_TIMEOUT);
/**
* What it does: