Production-ready animations, transitions, backgrounds, and scenes for Remotion
TypeScript ai-agentsclaude-codedemo-videomcpmcp-servermodel-context-protocolmotion-designproduct-demoreactremotionshadcnvideo
fix(render): evict stale done/error jobs from the registry map
dev_wandry0bfbb781fdd7
diff --git a/lib/server/__tests__/cleanup.test.ts b/lib/server/__tests__/cleanup.test.tsnew file mode 100644index 00000000..9fc19f74--- /dev/null+++ b/lib/server/__tests__/cleanup.test.ts@@ -0,0 +1,91 @@+import { describe, expect, it, mock } from "bun:test";+import type { RenderInput } from "@/lib/server/validate-input";++const mockRender = mock();++mock.module("@/lib/server/render", () => ({+ renderStarsVideo: mockRender,+}));++mock.module("node:fs/promises", () => ({+ mkdir: mock(() => Promise.resolve(undefined)),+}));++mock.module("server-only", () => ({}));++function makeInput(repo = "owner/repo"): RenderInput {+ return {+ repo,+ totalStars: 100,+ stargazers: [+ {+ login: "alice",+ avatarUrl: "https://avatars.githubusercontent.com/u/1",+ starredAt: "2021-01-01",+ },+ ],+ orientation: "horizontal",+ accentColor: "#ffbb00",+ speed: 1,+ theme: "light",+ };+}++const tick = () => new Promise<void>((r) => setTimeout(r, 0));++const { enqueueRender, getJob, listJobs } = await import(+ "@/lib/server/render-queue"+);+const { sweepOnce } = await import("@/lib/server/cleanup");++async function makeErrorJob(): Promise<string> {+ mockRender.mockRejectedValueOnce(new Error("Chromium crashed"));+ const jobId = enqueueRender(makeInput());+ await tick();+ await tick();+ return jobId;+}++describe("cleanup — sweepOnce registry eviction", () => {+ it("removes an error-status job older than the TTL", async () => {+ const jobId = await makeErrorJob();+ const job = listJobs().get(jobId)!;+ expect(job.status).toBe("error");++ job.createdAt = Date.now() - 700_000;++ await sweepOnce();++ expect(getJob(jobId)).toBeUndefined();+ });++ it("keeps an error-status job younger than the TTL", async () => {+ const jobId = await makeErrorJob();+ expect(listJobs().get(jobId)!.status).toBe("error");++ await sweepOnce();++ expect(getJob(jobId)).toBeDefined();+ });++ it("never evicts a rendering-status entry regardless of age", async () => {+ let releaseRender!: (v: string) => void;+ mockRender.mockImplementationOnce(+ () => new Promise<string>((resolve) => (releaseRender = resolve)),+ );++ const jobId = enqueueRender(makeInput());+ await tick();++ const job = listJobs().get(jobId)!;+ expect(job.status).toBe("rendering");+ job.createdAt = Date.now() - 10_000_000;++ await sweepOnce();++ expect(getJob(jobId)).toBeDefined();++ releaseRender("/tmp/out.mp4");+ await tick();+ });+});diff --git a/lib/server/cleanup.ts b/lib/server/cleanup.tsindex 2ae810b0..db649bcf 100644--- a/lib/server/cleanup.ts+++ b/lib/server/cleanup.ts@@ -1,8 +1,8 @@import "server-only";-import { readdir, rm, stat } from "node:fs/promises";+import * as fsPromises from "node:fs/promises";import path from "node:path";import { RENDER_WORK_DIR } from "./paths";-import { deleteJob } from "./render-queue";+import { deleteJob, listJobs } from "./render-queue";/*** Disk hygiene for the render work dir. Two mechanisms:@@ -27,22 +27,22 @@ function sweepIntervalMs(): number {export async function deleteJobFile(jobId: string): Promise<void> {const filePath = path.join(RENDER_WORK_DIR, `${jobId}.mp4`);try {- await rm(filePath, { force: true });+ await fsPromises.rm(filePath, { force: true });} finally {deleteJob(jobId);}}/** Remove every `.mp4` in the work dir older than the TTL. */-async function sweepOnce(): Promise<void> {+export async function sweepOnce(): Promise<void> {const ttl = ttlMs();const now = Date.now();let entries: string[];try {- entries = await readdir(RENDER_WORK_DIR);+ entries = await fsPromises.readdir(RENDER_WORK_DIR);} catch {- return; // dir not created yet — nothing to sweep+ entries = [];}await Promise.all(@@ -51,9 +51,9 @@ async function sweepOnce(): Promise<void> {.map(async (name) => {const filePath = path.join(RENDER_WORK_DIR, name);try {- const info = await stat(filePath);+ const info = await fsPromises.stat(filePath);if (now - info.mtimeMs > ttl) {- await rm(filePath, { force: true });+ await fsPromises.rm(filePath, { force: true });deleteJob(path.basename(name, ".mp4"));}} catch {@@ -61,6 +61,11 @@ async function sweepOnce(): Promise<void> {}}),);++ for (const [jobId, job] of listJobs()) {+ if (now - job.createdAt <= ttl) continue;+ if (job.status === "error" || job.status === "done") deleteJob(jobId);+ }}// Module-level guard: ensure the sweep timer is installed exactly once per