Skip to content

Open source CapCut alternative.

TypeScript

fix(worker): leave editor command jobs for the open card

David Leb392a50b3e4b
diff --git a/site/src/cut/worker/db.test.ts b/site/src/cut/worker/db.test.ts
index 3ae885b3..537f9d84 100644
--- a/site/src/cut/worker/db.test.ts
+++ b/site/src/cut/worker/db.test.ts
@@ -4,6 +4,7 @@ import { stubModule } from "@/lib/testing/stubModule";
// The account the charge point weighs every object against: free tier, no Pro.
const prisma = {
+ cutRenderJob: { findMany: mock(async (args: unknown): Promise<QueueRow[]> => { void args; return []; }) },
settingOverride: { findUnique: mock(async () => ({ value: { maxAttempts: 3 } })) },
user: { findUnique: mock(async () => ({ superUser: false })) },
proSubscription: { findUnique: mock(async () => null) },
@@ -15,7 +16,7 @@ const prisma = {
throw new Error("Unexpected transaction");
}) };
await stubModule<typeof import("@/lib/prisma")>("@/lib/prisma", import.meta.url, { prisma: prisma as never });
-const { registerObject, unregisterObjects } = await import("./db");
+const { claimNextJob, registerObject, unregisterObjects } = await import("./db");
const { FREE_STORAGE_BYTES } = await import("../server/cloud/limits");
const { STORAGE_FULL } = await import("../lib/operationFailure");
@@ -130,3 +131,59 @@ test("a re-registration that shrinks the object is charged its delta", async ()
expect(await registerObject({ ...input, bytes: 10 })).toBe("object");
expect(h.state()).toMatchObject({ usage: BigInt(110), stored: { bytes: BigInt(10) } });
});
+
+
+type QueueRow = {
+ id: string; userId: string; projectId: string; kind: string; state: string;
+ spec: null; outName: null;
+};
+function queueHarness(kinds: string[]) {
+ const rows: QueueRow[] = kinds.map((kind, i) => ({
+ id: `job-${i}`, userId: "user", projectId: "project", kind, state: "queued", spec: null, outName: null,
+ }));
+ const findMany = prisma.cutRenderJob.findMany.mockImplementation(async (args: unknown) => {
+ const { where, take } = args as { where: { state: string; kind: { in?: string[]; notIn?: string[] } }; take: number };
+ return rows.filter((row) => row.state === where.state
+ && (!where.kind.in || where.kind.in.includes(row.kind))
+ && (!where.kind.notIn || !where.kind.notIn.includes(row.kind))).slice(0, take);
+ });
+ const transaction = prisma.$transaction.mockImplementation(async (run: (tx: Prisma.TransactionClient) => Promise<unknown>) => {
+ const tx = { cutRenderJob: {
+ findFirst: async ({ where }: { where: { kind: string; state: string } }) => rows.find((row) => row.kind === where.kind && row.state === where.state) ?? null,
+ updateMany: async ({ where, data }: { where: { id: string; state: string }; data: { state: string } }) => {
+ const row = rows.find((row) => row.id === where.id && row.state === where.state);
+ if (!row) return { count: 0 };
+ row.state = data.state;
+ return { count: 1 };
+ },
+ findUnique: async ({ where }: { where: { id: string } }) => rows.find((row) => row.id === where.id) ?? null,
+ } };
+ return run(tx as unknown as Prisma.TransactionClient);
+ });
+ restores.push(() => { findMany.mockRestore(); transaction.mockRestore(); });
+ return rows;
+}
+
+test("the worker leaves editor command batches queued, even when they fill the oldest page", async () => {
+ const rows = queueHarness([...Array<string>(12).fill("commands"), "preview", "card", "hls"]);
+ for (const kind of ["preview", "card", "hls"]) expect((await claimNextJob())?.kind).toBe(kind);
+ expect(await claimNextJob()).toBeNull();
+ expect(rows.filter((row) => row.kind === "commands").every((row) => row.state === "queued")).toBe(true);
+});
+
+test("watched jobs precede background work and unsupported jobs stay queued", async () => {
+ const rows = queueHarness(["future_kind", "preview", "commands", "export", "import_url", "convert", "agent_turn"]);
+ for (const kind of ["export", "import_url", "convert", "agent_turn", "preview"]) {
+ expect((await claimNextJob())?.kind).toBe(kind);
+ }
+ expect(await claimNextJob()).toBeNull();
+ expect(rows.slice(0, 3).filter((row) => row.kind !== "preview").every((row) => row.state === "queued")).toBe(true);
+});
+
+test("a running project preview prevents a second preview claim while other work proceeds", async () => {
+ const rows = queueHarness(["preview", "preview", "card"]);
+ expect((await claimNextJob())?.id).toBe(rows[0].id);
+ expect((await claimNextJob())?.kind).toBe("card");
+ expect(await claimNextJob()).toBeNull();
+ expect(rows[1].state).toBe("queued");
+});
diff --git a/site/src/cut/worker/db.ts b/site/src/cut/worker/db.ts
index 3a4ed22c..bd8cb2a7 100644
--- a/site/src/cut/worker/db.ts
+++ b/site/src/cut/worker/db.ts
@@ -36,6 +36,52 @@ export interface ClaimedJob {
outName: string | null;
}
+/** The kinds someone is watching happen: an export, a URL import, or a chat
+ * turn has a progress surface on screen, while a hover proxy and a share card
+ * are background polish nobody is waiting on. */
+const WATCHED_KINDS = ["export", "import_url", "convert", "agent_turn"];
+
+/** Atomically claim the next queued job: the updateMany's state guard makes
+ * exactly one worker win each row, so replicas never double-run a job.
+ *
+ * Watched kinds go first, FIFO within a tier. Strict FIFO across all kinds let
+ * a proxy render queued moments earlier hold up an export the user is staring
+ * at — the wait was invisible in the UI, because the export's elapsed clock
+ * only starts once it is claimed. */
+export async function claimNextJob(): Promise<ClaimedJob | null> {
+ // Editor command batches stay queued for the card that owns their document.
+ for (const kind of [{ in: WATCHED_KINDS }, { in: ["preview", "card", "hls"] }]) {
+ const candidates = await prisma.cutRenderJob.findMany({
+ where: { state: "queued", kind },
+ orderBy: { createdAt: "asc" },
+ take: 10,
+ select: { id: true, userId: true, projectId: true, kind: true, spec: true, outName: true },
+ });
+ for (const c of candidates) {
+ const claimed = await prisma.$transaction(async (tx) => {
+ if (["preview", "card", "hls"].includes(c.kind)) {
+ const running = await tx.cutRenderJob.findFirst({
+ where: { userId: c.userId, projectId: c.projectId, kind: c.kind, state: "running" },
+ select: { id: true },
+ });
+ if (running) return null;
+ }
+ const updated = await tx.cutRenderJob.updateMany({
+ where: { id: c.id, state: "queued" },
+ data: { state: "running", claimedAt: new Date(), progress: 0, error: null },
+ });
+ if (updated.count !== 1) return null;
+ return tx.cutRenderJob.findUnique({
+ where: { id: c.id },
+ select: { id: true, userId: true, projectId: true, kind: true, spec: true, outName: true },
+ });
+ }, { isolationLevel: "Serializable" });
+ if (claimed) return claimed;
+ }
+ }
+ return null;
+}
+
/**
* Record a finished R2 object and keep the user's storage total in step, and
* hand back the row's id. The upsert makes re-registering the same key (a
diff --git a/site/src/cut/worker/main.ts b/site/src/cut/worker/main.ts
index 49269363..d3ec9938 100644
--- a/site/src/cut/worker/main.ts
+++ b/site/src/cut/worker/main.ts
@@ -1,6 +1,6 @@
import type { Prisma } from "@/generated/prisma/client";
import type { RenderHandle } from "../server/exportPipeline";
-import { prisma, type ClaimedJob } from "./db";
+import { claimNextJob, prisma, type ClaimedJob } from "./db";
import { overlayKeysOf, runExportJob } from "./exportJob";
import { runHlsJob } from "./hlsJob";
import { runConvertJob } from "./convertJob";
@@ -72,51 +72,6 @@ let stopping = false;
const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
-/** The kinds someone is watching happen: an export, a URL import, or a chat
- * turn has a progress surface on screen, while a hover proxy and a share card
- * are background polish nobody is waiting on. */
-const WATCHED_KINDS = ["export", "import_url", "convert", "agent_turn"];
-
-/** Atomically claim the next queued job: the updateMany's state guard makes
- * exactly one worker win each row, so replicas never double-run a job.
- *
- * Watched kinds go first, FIFO within a tier. Strict FIFO across all kinds let
- * a proxy render queued moments earlier hold up an export the user is staring
- * at — the wait was invisible in the UI, because the export's elapsed clock
- * only starts once it is claimed. */
-async function claimNext(): Promise<ClaimedJob | null> {
- for (const kind of [{ in: WATCHED_KINDS }, { notIn: WATCHED_KINDS }]) {
- const candidates = await prisma.cutRenderJob.findMany({
- where: { state: "queued", kind },
- orderBy: { createdAt: "asc" },
- take: 10,
- select: { id: true, userId: true, projectId: true, kind: true, spec: true, outName: true },
- });
- for (const c of candidates) {
- const claimed = await prisma.$transaction(async (tx) => {
- if (["preview", "card", "hls"].includes(c.kind)) {
- const running = await tx.cutRenderJob.findFirst({
- where: { userId: c.userId, projectId: c.projectId, kind: c.kind, state: "running" },
- select: { id: true },
- });
- if (running) return null;
- }
- const updated = await tx.cutRenderJob.updateMany({
- where: { id: c.id, state: "queued" },
- data: { state: "running", claimedAt: new Date(), progress: 0, error: null },
- });
- if (updated.count !== 1) return null;
- return tx.cutRenderJob.findUnique({
- where: { id: c.id },
- select: { id: true, userId: true, projectId: true, kind: true, spec: true, outName: true },
- });
- }, { isolationLevel: "Serializable" });
- if (claimed) return claimed;
- }
- }
- return null;
-}
-
/** Requeue "running" rows whose heartbeat (updatedAt) has gone quiet: their
* worker died without SIGTERM (OOM, host kill), so nothing else will ever
* settle them. Own in-flight jobs are excluded by id. */
@@ -319,7 +274,7 @@ async function main(): Promise<void> {
lastSweep = Date.now();
await sweepStaleRunning();
}
- job = await claimNext();
+ job = await claimNextJob();
failingSince = null;
} catch (err) {
console.error("[cut-worker] claim failed:", err instanceof Error ? err.message : err);