Skip to content

Commit 4d177e7

Browse files
committed
fix: cap job drain under Vercel 300s timeout
Limit process route to 2 rounds / ~8 jobs with a 240s wall so Hobby functions exit cleanly; stale reclaim handles interrupted work.
1 parent 2f10455 commit 4d177e7

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

app/api/v1/internal/jobs/process/route.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@ import { assertCronAuth } from '@/lib/api/cron-auth';
22
import { processAgentJobs } from '@/lib/jobs/process';
33
import { runErasureBatch } from '@/lib/data-rights/erasure';
44

5-
/** One cron hit should clear backlog, not just 10 jobs every 5 minutes. */
6-
const DRAIN_ROUNDS = 4;
5+
/**
6+
* Hobby functions die at ~300s. Keep headroom: GHA curl max-time is 280s.
7+
* More backlog drains on the next 5m cron + reclaimStaleRunningJobs.
8+
*/
9+
const DRAIN_ROUNDS = 2;
10+
const WALL_MS = 240_000;
711

812
async function processJobs(limit?: number) {
13+
const started = Date.now();
914
const erasures = await runErasureBatch();
1015
const totals = { processed: 0, succeeded: 0, failed: 0, reclaimed: 0 };
1116
for (let i = 0; i < DRAIN_ROUNDS; i += 1) {
12-
const result = await processAgentJobs({ limit });
17+
if (Date.now() - started > WALL_MS) break;
18+
const result = await processAgentJobs({
19+
limit: limit ?? 8,
20+
});
1321
totals.processed += result.processed;
1422
totals.succeeded += result.succeeded;
1523
totals.failed += result.failed;
1624
totals.reclaimed += result.reclaimed;
1725
if (result.processed === 0) break;
1826
}
19-
return Response.json({ ...totals, erasures });
27+
return Response.json({ ...totals, erasures, elapsed_ms: Date.now() - started });
2028
}
2129

2230
export async function GET(request: Request) {

lib/jobs/process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Database, Json } from "@/supabase/database.types";
66

77
type AgentJobRow = Database["public"]["Tables"]["agent_jobs"]["Row"];
88

9-
const DEFAULT_BATCH_SIZE = 15;
9+
const DEFAULT_BATCH_SIZE = 8;
1010
const MAX_ATTEMPTS = 3;
1111
const RETRY_BASE_DELAY_MS = 60_000;
1212
const RETRY_MAX_DELAY_MS = 30 * 60_000;

0 commit comments

Comments
 (0)