mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(studio-server): the cancel route replies once the adapter's stop has settled (#7221)
* fix(studio-server): the cancel route replies once the adapter's stop has settled * fix(studio-server): shorten the cancel route comments * fix(studio-server): drop the added cancel comments so the comment share stays down * fix(studio-server): the cancel route clears its timer when the stop throws
This commit is contained in:
@@ -739,6 +739,34 @@ describe("POST /render/:jobId/cancel", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("replies only once the adapter's stop has settled", async () => {
|
||||
const { adapter, rendersDir } = createAdapter(vi.fn());
|
||||
const baseStartRender = adapter.startRender.bind(adapter);
|
||||
let settled = false;
|
||||
adapter.startRender = (opts) => {
|
||||
const state = baseStartRender(opts);
|
||||
state.cancel = () =>
|
||||
new Promise<void>((done) =>
|
||||
setTimeout(() => {
|
||||
settled = true;
|
||||
state.status = "failed";
|
||||
done();
|
||||
}, 30),
|
||||
);
|
||||
return state;
|
||||
};
|
||||
const app = new Hono();
|
||||
registerRenderRoutes(app, adapter);
|
||||
try {
|
||||
const jobId = await startJob(app);
|
||||
const res = await app.request(`http://localhost/render/${jobId}/cancel`, { method: "POST" });
|
||||
expect(settled).toBe(true);
|
||||
expect(((await res.json()) as { status: string }).status).toBe("failed");
|
||||
} finally {
|
||||
rmSync(rendersDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("does not cancel a job that already completed", async () => {
|
||||
const spy = vi.fn();
|
||||
const states: Array<{ status: string }> = [];
|
||||
|
||||
@@ -58,6 +58,7 @@ function contentDispositionHeader(disposition: "inline" | "attachment", filename
|
||||
export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void {
|
||||
// Scoped job store — not shared across createStudioApi() calls
|
||||
const renderJobs = new Map<string, RenderJobState>();
|
||||
const CANCEL_SETTLE_MS = 45_000;
|
||||
|
||||
const nextFreeRenderJobId = (baseId: string, ext: string, rendersDir: string): string => {
|
||||
let jobId = baseId;
|
||||
@@ -246,13 +247,19 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
|
||||
|
||||
// Cancel an in-flight render. Marks the job cancelled immediately (so the
|
||||
// SSE stream terminates) and invokes the adapter's abort hook when present.
|
||||
api.post("/render/:jobId/cancel", (c) => {
|
||||
api.post("/render/:jobId/cancel", async (c) => {
|
||||
const { jobId } = c.req.param();
|
||||
const job = renderJobs.get(jobId);
|
||||
if (!job) return c.json({ error: "not found" }, 404);
|
||||
if (job.status === "rendering") {
|
||||
job.status = "cancelled";
|
||||
job.cancel?.();
|
||||
let giveUp: ReturnType<typeof setTimeout> | undefined;
|
||||
const late = new Promise((done) => (giveUp = setTimeout(done, CANCEL_SETTLE_MS)));
|
||||
try {
|
||||
await Promise.race([job.cancel?.(), late]);
|
||||
} finally {
|
||||
clearTimeout(giveUp);
|
||||
}
|
||||
}
|
||||
return c.json({ status: job.status });
|
||||
});
|
||||
|
||||
@@ -25,7 +25,7 @@ export interface RenderJobState {
|
||||
* stop an in-flight render; adapters that can't abort may omit it (the
|
||||
* route still marks the job cancelled so the SSE stream terminates).
|
||||
*/
|
||||
cancel?: () => void;
|
||||
cancel?: () => void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface MediaProcessingJobState {
|
||||
|
||||
Reference in New Issue
Block a user