Skip to content

Commit 92943d9

Browse files
fix(studio-server): two exports in the same second get separate jobs (heygen-com#7053)
* fix(studio-server): two exports in the same second get separate jobs * fix(studio-server): name the same-second job id helper instead of commenting it
1 parent 81b93cf commit 92943d9

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎packages/studio-server/src/routes/render.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@ function buildApp(spy: ReturnType<typeof vi.fn>): {
4444
return { app, rendersDir, cleanup: () => rmSync(rendersDir, { recursive: true, force: true }) };
4545
}
4646

47+
describe("POST /projects/:id/render — job ids", () => {
48+
it("gives two exports started in the same second their own job and output file", async () => {
49+
vi.useFakeTimers({ toFake: ["Date"] });
50+
vi.setSystemTime(new Date("2026-11-02T12:00:00Z"));
51+
const spy = vi.fn();
52+
const { app, rendersDir, cleanup } = buildApp(spy);
53+
try {
54+
const start = () =>
55+
app.request("http://localhost/projects/demo/render", {
56+
method: "POST",
57+
headers: { "content-type": "application/json" },
58+
body: JSON.stringify({ fps: 30, quality: "standard", format: "mp4" }),
59+
});
60+
const [first, second] = await Promise.all([start(), start()]);
61+
const ids = [
62+
((await first.json()) as { jobId: string }).jobId,
63+
((await second.json()) as { jobId: string }).jobId,
64+
];
65+
66+
expect(new Set(ids).size).toBe(2);
67+
expect(spy.mock.calls.map(([opts]) => opts.outputPath)).toEqual(
68+
ids.map((id) => join(rendersDir, `${id}.mp4`)),
69+
);
70+
} finally {
71+
vi.useRealTimers();
72+
cleanup();
73+
}
74+
});
75+
});
76+
4777
describe("GET /projects/:id/renders — stale sidecar status", () => {
4878
it("does not mark an existing output failed from stale metadata", async () => {
4979
const spy = vi.fn();

‎packages/studio-server/src/routes/render.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
5858
// Scoped job store — not shared across createStudioApi() calls
5959
const renderJobs = new Map<string, RenderJobState & { createdAt: number }>();
6060

61+
const nextFreeRenderJobId = (baseId: string, ext: string, rendersDir: string): string => {
62+
let jobId = baseId;
63+
for (let n = 2; renderJobs.has(jobId) || existsSync(join(rendersDir, `${jobId}${ext}`)); n++) {
64+
jobId = `${baseId}-${n}`;
65+
}
66+
return jobId;
67+
};
68+
6169
// TTL cleanup for completed jobs (5 minutes)
6270
const TTL_MS = 300_000;
6371
const CLEANUP_INTERVAL_MS = 60_000;
@@ -178,11 +186,14 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
178186
? "off"
179187
: requestedPageMotionBlur;
180188

181-
const now = new Date();
182-
const jobId = `${project.id}_${formatRenderOutputTimestamp(now)}`;
183189
const rendersDir = adapter.rendersDir(project);
184190
mkdirWithinProject(project.dir, rendersDir);
185191
const ext = FORMAT_EXT[format] ?? ".mp4";
192+
const jobId = nextFreeRenderJobId(
193+
`${project.id}_${formatRenderOutputTimestamp(new Date())}`,
194+
ext,
195+
rendersDir,
196+
);
186197
const outputPath = join(rendersDir, `${jobId}${ext}`);
187198

188199
const jobState = adapter.startRender({

0 commit comments

Comments
 (0)