Skip to content

Open source CapCut alternative.

TypeScript

feat(site): queue Cut exports and expose an account-scoped job feed [rebuild]

David Lef07b545d96bc
diff --git a/site/src/cut/server/http/export.ts b/site/src/cut/server/http/export.ts
index 1b6357af..72a4dc48 100644
--- a/site/src/cut/server/http/export.ts
+++ b/site/src/cut/server/http/export.ts
@@ -1,4 +1,4 @@
-import { cancelJob, createJob, getJob, listJobsForProject } from "../jobs";
+import { cancelJob, createJob, getJob, listAllJobs, listJobsForProject } from "../jobs";
import { serveFileRange } from "../serveFile";
/** Export rendering: start a job, poll it, cancel it, download the result. */
@@ -9,6 +9,12 @@ export const exportApi = {
return Response.json(listJobsForProject(id));
},
+ /** Every export job across every project, in start order — the feed the
+ * app-wide exports dock polls so it shows the same queue in every tab. */
+ activeAll() {
+ return Response.json(listAllJobs());
+ },
+
async create(req: Request) {
try {
const form = await req.formData();
diff --git a/site/src/cut/server/http/routes.ts b/site/src/cut/server/http/routes.ts
index 92bc0fe3..56c46cda 100644
--- a/site/src/cut/server/http/routes.ts
+++ b/site/src/cut/server/http/routes.ts
@@ -71,6 +71,7 @@ export const CUT_ROUTES: CutRoute[] = [
{ method: "GET", path: "/api/cut/library/media/:file", handler: (req, p) => libraryApi.serveMedia(req, { file: p.file }) },
{ method: "DELETE", path: "/api/cut/library/:id", handler: (req, p) => libraryApi.remove(req, { id: p.id }) },
+ { method: "GET", path: "/api/cut/export-jobs", handler: () => exportApi.activeAll() },
{ method: "POST", path: "/api/cut/export", handler: (req) => exportApi.create(req) },
{ method: "GET", path: "/api/cut/export/:jobId", handler: (req, p) => exportApi.status(req, { jobId: p.jobId }) },
{ method: "DELETE", path: "/api/cut/export/:jobId", handler: (req, p) => exportApi.cancel(req, { jobId: p.jobId }) },
diff --git a/site/src/cut/server/jobRegistry.ts b/site/src/cut/server/jobRegistry.ts
index c4bd506f..3e897e35 100644
--- a/site/src/cut/server/jobRegistry.ts
+++ b/site/src/cut/server/jobRegistry.ts
@@ -15,10 +15,14 @@ export interface JobRegistry<J> {
export function createJobRegistry<J extends { id: string; status: string }>(
globalKey: string,
- opts: { maxJobs?: number; retireMs?: number } = {}
+ opts: { maxJobs?: number; retireMs?: number; isTerminal?: (job: J) => boolean } = {}
): JobRegistry<J> {
const maxJobs = opts.maxJobs ?? 50;
const retireMs = opts.retireMs ?? 10 * 60 * 1000;
+ // What counts as backlog to evict. Default: anything not running. A registry
+ // with a queue passes its own predicate so waiting ("queued") jobs are never
+ // mistaken for settled backlog and dropped before they get a slot.
+ const isTerminal = opts.isTerminal ?? ((j: J) => j.status !== "running");
const g = globalThis as unknown as Record<string, Map<string, J> | undefined>;
const jobs = (g[globalKey] ??= new Map<string, J>());
const retiring = new Set<string>();
@@ -36,7 +40,7 @@ export function createJobRegistry<J extends { id: string; status: string }>(
jobs.delete(job.id);
retiring.delete(job.id);
}, retireMs).unref();
- const terminal = [...jobs.values()].filter((j) => j.status !== "running");
+ const terminal = [...jobs.values()].filter(isTerminal);
for (let i = 0; i < terminal.length - maxJobs; i++) {
jobs.delete(terminal[i].id);
retiring.delete(terminal[i].id);
diff --git a/site/src/cut/server/jobs.ts b/site/src/cut/server/jobs.ts
index dc261d0b..378d456c 100644
--- a/site/src/cut/server/jobs.ts
+++ b/site/src/cut/server/jobs.ts
@@ -5,6 +5,7 @@ import path from "node:path";
import { assertLocalRuntime } from "./local-only";
import { createJobRegistry } from "./jobRegistry";
import { exportsDir, mediaPath, projectDir, readProject } from "./projects";
+import { currentCutUser } from "./userScope";
import { atempoChain, hasStream, num } from "./util";
import { projectFadeSeconds, TRANSITION_ZOOM } from "../lib/types";
@@ -93,13 +94,23 @@ export interface ExportSpec {
export interface Job {
id: string;
+ /** The Donkey account that started the job. The cross-project feed is scoped
+ * to it, so accounts sharing a Mac never see each other's exports. */
+ user: string;
projectId: string;
+ /** Shown in the exports dock; the engine assigns it from the project doc. */
+ projectName: string;
/** "preview" is the internal hover-proxy render; only "export" jobs are
- * surfaced to the client for reconnect/download. */
+ * surfaced to the client dock for progress/download. */
target: "export" | "preview";
- status: "running" | "done" | "error";
+ /** "queued" waits for a running slot; the rest track the ffmpeg run. */
+ status: "queued" | "running" | "done" | "error";
progress: number; // 0..1
error?: string;
+ /** When the job was created (queue order) and when it actually began the
+ * encode (elapsed clock in the dock). */
+ createdAt: number;
+ startedAt?: number;
tmpDir: string;
outPath: string;
outName: string;
@@ -107,35 +118,104 @@ export interface Job {
log: string[];
}
-const MAX_RUNNING = 2; // concurrent ffmpeg exports
-// Survives dev-server module reloads; caps the terminal backlog.
-const { jobs, runningCount, retire } = createJobRegistry<Job>("__veditorJobs");
+const MAX_RUNNING = 2; // concurrent ffmpeg exports; extra exports queue behind them
+// Survives dev-server module reloads; caps the terminal backlog. Queued jobs
+// are active work, not backlog, so they are exempt from eviction.
+const { jobs, runningCount, retire } = createJobRegistry<Job>("__veditorJobs", {
+ isTerminal: (j) => j.status === "done" || j.status === "error",
+});
+
+// Export jobs waiting for a running slot, oldest first. Held on globalThis so a
+// dev-server module reload doesn't strand a queued render. Preview proxies never
+// queue — they run when a slot is free and are rejected otherwise.
+interface Pending {
+ job: Job;
+ spec: ExportSpec;
+}
+const g = globalThis as unknown as { __veditorPending?: Pending[] };
+const pending: Pending[] = (g.__veditorPending ??= []);
+
+/** Promote queued exports into free running slots, oldest first. Called after
+ * every enqueue and every settle, so the queue always drains to capacity. */
+function pump() {
+ while (runningCount() < MAX_RUNNING && pending.length > 0) {
+ const next = pending.shift()!;
+ if (next.job.status !== "queued") continue; // canceled while waiting
+ startRun(next.job, next.spec);
+ }
+}
+
+/** Move a job from queued to running and drive its ffmpeg render. Its settle
+ * frees the slot and pumps the queue. */
+function startRun(job: Job, spec: ExportSpec) {
+ job.status = "running";
+ job.startedAt = Date.now();
+ void runExport(job, spec)
+ .catch((err: unknown) => {
+ job.status = "error";
+ job.error = err instanceof Error ? err.message : String(err);
+ void rm(job.outPath, { force: true }); // no half-written files in exports/
+ })
+ .finally(() => {
+ void rm(job.tmpDir, { recursive: true, force: true }); // overlay tmp, win or lose
+ retire(job);
+ pump();
+ });
+}
export function getJob(id: string) {
return jobs.get(id);
}
-/** Running and recently-settled export jobs for a project, so a client that
- * reopened (or reloaded) can reconnect to an export still in flight. */
+/** One export job's dock view: enough for the client to show progress, elapsed,
+ * queue position, and the finished file's actions. Previews stay internal. */
+function jobView(j: Job) {
+ return {
+ id: j.id,
+ projectId: j.projectId,
+ projectName: j.projectName,
+ status: j.status,
+ progress: j.progress,
+ outName: j.outName || undefined,
+ error: j.error,
+ createdAt: j.createdAt,
+ startedAt: j.startedAt,
+ };
+}
+
+/** Export jobs (queued, running, or recently settled) for one project. */
export function listJobsForProject(projectId: string) {
return [...jobs.values()]
.filter((j) => j.projectId === projectId && j.target !== "preview")
- .map((j) => ({
- id: j.id,
- status: j.status,
- progress: j.progress,
- outName: j.outName,
- error: j.error,
- }));
+ .map(jobView);
+}
+
+/** Every export job for the requesting account, across all its projects — the
+ * source of truth the app-wide exports dock reflects, so it shows the same set
+ * in every tab. Scoped to the account so a shared Mac never crosses feeds. */
+export function listAllJobs() {
+ const user = currentCutUser();
+ return [...jobs.values()]
+ .filter((j) => j.target !== "preview" && j.user === user)
+ .sort((a, b) => a.createdAt - b.createdAt)
+ .map(jobView);
}
export function cancelJob(id: string) {
const job = jobs.get(id);
- if (job?.proc && job.status === "running") {
+ if (!job) return;
+ if (job.status === "running" && job.proc) {
job.proc.kill("SIGKILL");
job.status = "error";
job.error = "Export canceled.";
retire(job);
+ } else if (job.status === "queued") {
+ // Never started: drop it from the queue and settle it so the dock clears.
+ job.status = "error";
+ job.error = "Export canceled.";
+ const i = pending.findIndex((p) => p.job.id === id);
+ if (i >= 0) pending.splice(i, 1);
+ retire(job);
}
}
@@ -172,14 +252,23 @@ export async function createJob(form: FormData): Promise<Job> {
assertLocalRuntime();
const spec = JSON.parse(String(form.get("spec"))) as ExportSpec;
const id = crypto.randomUUID().slice(0, 12);
+ const user = currentCutUser();
const preview = spec.target === "preview";
- if (runningCount() >= MAX_RUNNING) {
+
+ // Previews are best-effort hover proxies: they take a free slot or bow out,
+ // never queueing. Queued exports already hold every slot (pump keeps the
+ // registry full while any wait), so this cap check also stops a preview from
+ // jumping the export queue.
+ if (preview && runningCount() >= MAX_RUNNING) {
const job: Job = {
id,
+ user,
projectId: spec.projectId,
- target: preview ? "preview" : "export",
+ projectName: "",
+ target: "preview",
status: "error",
progress: 0,
+ createdAt: Date.now(),
tmpDir: "",
outPath: "",
outName: "",
@@ -190,22 +279,27 @@ export async function createJob(form: FormData): Promise<Job> {
retire(job);
return job;
}
+
const job: Job = {
id,
+ user,
projectId: spec.projectId,
+ projectName: "",
target: preview ? "preview" : "export",
- status: "running",
+ status: "queued",
progress: 0,
+ createdAt: Date.now(),
tmpDir: "",
outPath: "",
outName: "",
log: [],
};
- jobs.set(id, job); // reserve the concurrency slot before the first await
+ jobs.set(id, job);
try {
const doc = await readProject(spec.projectId);
if (!doc) throw new Error("Project not found.");
+ job.projectName = doc.name;
if (preview) job.outName = "preview.mp4";
else await claimExportName(job, doc.name);
job.outPath = path.join(
@@ -220,16 +314,20 @@ export async function createJob(form: FormData): Promise<Job> {
await writeFile(path.join(job.tmpDir, path.basename(key)), Buffer.from(await value.arrayBuffer()));
}
}
- void runExport(job, spec)
- .catch((err: unknown) => {
+ if (preview) {
+ // Re-check the slot: it may have been taken during the prep above. A
+ // dropped preview just refreshes later, so bow out instead of racing.
+ if (runningCount() < MAX_RUNNING) startRun(job, spec);
+ else {
job.status = "error";
- job.error = err instanceof Error ? err.message : String(err);
- void rm(job.outPath, { force: true }); // no half-written files in exports/
- })
- .finally(() => {
- void rm(job.tmpDir, { recursive: true, force: true }); // overlay tmp, win or lose
+ job.error = "Another export is already running — wait for it to finish.";
+ void rm(job.tmpDir, { recursive: true, force: true });
retire(job);
- });
+ }
+ } else {
+ pending.push({ job, spec });
+ pump();
+ }
} catch (err) {
job.status = "error";
job.error = err instanceof Error ? err.message : String(err);