-
Notifications
You must be signed in to change notification settings - Fork 668
Replace Vercel Workflow with Supabase Queues + duplicate detection #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pontusab
wants to merge
2
commits into
main
Choose a base branch
from
feat/replace-workflow-with-supabase-queues
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
apps/cursor/src/app/api/queue/plugin-scans/drain/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { type NextRequest, NextResponse } from "next/server"; | ||
| import { requireCronAuth } from "@/lib/cron-auth"; | ||
| import { | ||
| archivePluginScan, | ||
| PLUGIN_SCAN_QUEUE, | ||
| readNextPluginScan, | ||
| } from "@/lib/plugins/queue"; | ||
| import { | ||
| FatalScanError, | ||
| markScanFailed, | ||
| runPluginScan, | ||
| } from "@/lib/plugins/scan"; | ||
|
|
||
| // Vercel max for Pro / Enterprise + Fluid Compute (default since 2025) is 800s. | ||
| // Source: https://vercel.com/docs/functions/configuring-functions/duration | ||
| // | ||
| // The `Agent.prompt` step can take 1–3 minutes for a typical plugin; the git | ||
| // clone is bounded by CLONE_TIMEOUT_MS (60s) inside scan.ts. 800s gives us | ||
| // generous headroom for the worst-case agent run. | ||
| export const dynamic = "force-dynamic"; | ||
| export const maxDuration = 800; | ||
|
|
||
| // Visibility timeout: how long the message is invisible to other consumers | ||
| // after a successful `read`. Set comfortably longer than `maxDuration` so we | ||
| // can never hand the same message to a second drain invocation while the | ||
| // first one is still running. | ||
| const VT_SECONDS = 900; | ||
|
|
||
| // Bury after this many delivery attempts. With per-cron `n=1` and a 1-min | ||
| // schedule, this means a poisonous message stays in the queue for ~5 min | ||
| // after `read_ct=1` (we only see read_ct on the next read after the VT | ||
| // expires) before we mark the plugin errored and stop retrying. | ||
| const MAX_ATTEMPTS = 5; | ||
|
|
||
| function logInfo(msg: string, meta?: Record<string, unknown>) { | ||
| console.log(`[scan-drain] ${msg}${meta ? ` ${JSON.stringify(meta)}` : ""}`); | ||
| } | ||
|
|
||
| function logError(msg: string, err: unknown) { | ||
| const detail = | ||
| err instanceof Error | ||
| ? { name: err.name, message: err.message, stack: err.stack } | ||
| : { value: String(err) }; | ||
| console.error(`[scan-drain] ${msg}`, detail); | ||
| } | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| const unauthorized = requireCronAuth(request); | ||
| if (unauthorized) return unauthorized; | ||
|
|
||
| let msg: Awaited<ReturnType<typeof readNextPluginScan>>; | ||
| try { | ||
| msg = await readNextPluginScan(VT_SECONDS); | ||
| } catch (err) { | ||
| logError("readNextPluginScan failed", err); | ||
| return NextResponse.json( | ||
| { ok: false, error: "queue_read_failed" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| if (!msg) { | ||
| return NextResponse.json({ | ||
| ok: true, | ||
| queue: PLUGIN_SCAN_QUEUE, | ||
| drained: 0, | ||
| }); | ||
| } | ||
|
|
||
| const { msg_id, read_ct, message } = msg; | ||
| const pluginId = message.plugin_id; | ||
|
|
||
| if (!pluginId || typeof pluginId !== "string") { | ||
| // Malformed payload — archive it so it doesn't keep getting retried. | ||
| logError( | ||
| "malformed message; archiving", | ||
| new Error(JSON.stringify(message)), | ||
| ); | ||
| await archivePluginScan(msg_id).catch((err) => | ||
| logError("archive (malformed) failed", err), | ||
| ); | ||
| return NextResponse.json( | ||
| { ok: false, archived: msg_id, reason: "malformed_message" }, | ||
| { status: 200 }, | ||
| ); | ||
| } | ||
|
|
||
| if (read_ct > MAX_ATTEMPTS) { | ||
| logInfo("exceeded MAX_ATTEMPTS; burying", { | ||
| pluginId, | ||
| msg_id, | ||
| read_ct, | ||
| max: MAX_ATTEMPTS, | ||
| }); | ||
| await markScanFailed(pluginId, `Exceeded ${MAX_ATTEMPTS} scan attempts`); | ||
| await archivePluginScan(msg_id); | ||
|
pontusab marked this conversation as resolved.
|
||
| return NextResponse.json({ | ||
| ok: true, | ||
| buried: pluginId, | ||
| msg_id, | ||
| read_ct, | ||
| }); | ||
| } | ||
|
|
||
| logInfo("processing", { pluginId, msg_id, read_ct }); | ||
|
|
||
| try { | ||
| await runPluginScan(pluginId); | ||
| await archivePluginScan(msg_id); | ||
| logInfo("scanned ok", { pluginId, msg_id }); | ||
| return NextResponse.json({ ok: true, scanned: pluginId, msg_id }); | ||
| } catch (err) { | ||
| if (err instanceof FatalScanError) { | ||
| // runPluginScan already wrote `scan_status='error'` via its compensation | ||
| // path. Archive so the message doesn't get retried. | ||
| logError("fatal; archiving", err); | ||
| await archivePluginScan(msg_id).catch((archiveErr) => | ||
| logError("archive (fatal) failed", archiveErr), | ||
| ); | ||
| return NextResponse.json( | ||
| { | ||
| ok: false, | ||
| fatal: true, | ||
| pluginId, | ||
| msg_id, | ||
| error: err.message, | ||
| }, | ||
| { status: 200 }, | ||
| ); | ||
| } | ||
|
|
||
| // Retryable: do NOT archive. The pgmq visibility timeout (VT_SECONDS) | ||
| // expires and the next cron tick re-reads the message with read_ct + 1. | ||
| logError("retryable; leaving message for VT to expire", err); | ||
| return NextResponse.json( | ||
| { | ||
| ok: false, | ||
| retryable: true, | ||
| pluginId, | ||
| msg_id, | ||
| read_ct, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.