diff --git a/.server-changes/background-worker-version-endpoint-tasks.md b/.server-changes/background-worker-version-endpoint-tasks.md new file mode 100644 index 0000000000..2499bcc3a0 --- /dev/null +++ b/.server-changes/background-worker-version-endpoint-tasks.md @@ -0,0 +1,6 @@ +--- +area: webapp +type: improvement +--- + +Speed up retrieving a background worker by version. The endpoint no longer runs a slow lookup that scanned the full task table for large deployments; it now reuses data it already loads, so the response is the same but returns much faster. diff --git a/apps/webapp/app/routes/api.v1.projects.$projectRef.background-workers.$envSlug.$version.ts b/apps/webapp/app/routes/api.v1.projects.$projectRef.background-workers.$envSlug.$version.ts index 72c4549279..99e1dcc9ac 100644 --- a/apps/webapp/app/routes/api.v1.projects.$projectRef.background-workers.$envSlug.$version.ts +++ b/apps/webapp/app/routes/api.v1.projects.$projectRef.background-workers.$envSlug.$version.ts @@ -45,15 +45,7 @@ export async function loader({ params, request }: LoaderFunctionArgs) { }, include: { tasks: true, - files: { - include: { - tasks: { - select: { - slug: true, - }, - }, - }, - }, + files: true, }, }); @@ -61,6 +53,19 @@ export async function loader({ params, request }: LoaderFunctionArgs) { return json({ error: "Background worker not found" }, { status: 404 }); } + // Group task slugs by fileId from the already-loaded tasks (which are fetched + // via the indexed workerId relation) instead of loading files.tasks, which + // queries BackgroundWorkerTask by the unindexed fileId column. + const taskSlugsByFileId = new Map>(); + for (const task of backgroundWorker.tasks) { + if (!task.fileId) { + continue; + } + const slugs = taskSlugsByFileId.get(task.fileId) ?? new Set(); + slugs.add(task.slug); + taskSlugsByFileId.set(task.fileId, slugs); + } + return json({ id: backgroundWorker.friendlyId, version: backgroundWorker.version, @@ -82,7 +87,7 @@ export async function loader({ params, request }: LoaderFunctionArgs) { filePath: file.filePath, contentHash: file.contentHash, contents: decompressContent(file.contents), - tasks: Array.from(new Set(file.tasks.map((task) => task.slug))), + tasks: Array.from(taskSlugsByFileId.get(file.id) ?? []), })), }); } catch (error) {