Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(media-server): stop waiting on cap.so between web upload processing stages

Richie McIlroyfe2b9f8fe345
diff --git a/apps/media-server/src/__tests__/routes/lifecycle.test.ts b/apps/media-server/src/__tests__/routes/lifecycle.test.ts
index dfd39edce..11617c505 100644
--- a/apps/media-server/src/__tests__/routes/lifecycle.test.ts
+++ b/apps/media-server/src/__tests__/routes/lifecycle.test.ts
@@ -445,6 +445,54 @@ describe("process jobs", () => {
expect(preview.gifs.every((gif) => !existsSync(gif.path))).toBe(true);
});
+ test("does not wait on slow progress webhooks and still delivers completion last", async () => {
+ const received: string[] = [];
+ const hook = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ async fetch(request) {
+ const { phase } = (await request.json()) as { phase: string };
+ received.push(phase);
+ await new Promise((resolve) => setTimeout(resolve, 500));
+ return Response.json({ success: true });
+ },
+ });
+ try {
+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(() =>
+ tempFileWith(".mp4", "input"),
+ );
+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue({
+ ...metadata,
+ duration: 10,
+ });
+ spyOn(mediaVideo, "processVideo").mockImplementation(() =>
+ tempFileWith(".mp4", "output"),
+ );
+ spyOn(mediaVideo, "uploadFileToS3").mockResolvedValue({});
+ const started = Date.now();
+ const response = await app.fetch(
+ post("/video/process", {
+ videoId: "video-slow-webhooks",
+ userId: "user-id",
+ videoUrl: "https://example.com/input.mp4",
+ outputPresignedUrl: "https://example.com/output.mp4",
+ webhookUrl: `http://127.0.0.1:${hook.port}/webhook`,
+ }),
+ );
+ const { jobId } = await response.json();
+ createdJobs.push(jobId);
+ await waitFor(() => getJob(jobId)?.phase === "complete", 5_000);
+ await waitFor(() => received.at(-1) === "complete", 5_000);
+ // Waiting on each 500 ms progress webhook delays completion by about 2 s; without waiting it lands in about 0.5 s.
+ expect(Date.now() - started).toBeLessThan(1_500);
+ expect(received[0]).toBe("downloading");
+ expect(received.filter((phase) => phase === "complete")).toHaveLength(1);
+ expect(received.at(-1)).toBe("complete");
+ } finally {
+ hook.stop(true);
+ }
+ });
+
test("releases the job slot and temp files when processing fails", async () => {
const before = getActiveVideoProcessCount();
let input: TempFileHandle | undefined;
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index 1bedd8777..43cefa5a2 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -1530,7 +1530,9 @@ async function processVideoAsync(
progress: 0,
message: "Downloading video...",
});
- await sendWebhook(job);
+ // Progress webhooks only update the upload row, so don't wait on cap.so between stages. sendWebhook keeps one
+ // delivery in flight per job and always sends the latest state, so the awaited terminal webhook still lands last.
+ void sendWebhook(job);
const input = await withJobHeartbeat(jobId, () =>
downloadVideoToTemp(
@@ -1562,7 +1564,7 @@ async function processVideoAsync(
const sourcePresignedUrl = options.sourcePresignedUrl;
if (sourcePresignedUrl || options.sourceUpload) {
updateJob(jobId, { message: "Saving original video..." });
- await sendWebhook(job);
+ void sendWebhook(job);
await withJobHeartbeat(jobId, () =>
options.sourceUpload
? uploadFileToStorage(
@@ -1594,7 +1596,7 @@ async function processVideoAsync(
progress: 5,
message: "Analyzing video...",
});
- await sendWebhook(job);
+ void sendWebhook(job);
const { metadata, repairedFile } = await withJobHeartbeat(jobId, () =>
options.existingOutput
@@ -1622,7 +1624,7 @@ async function processVideoAsync(
? "Processing repaired video..."
: "Processing video...",
});
- await sendWebhook(job);
+ void sendWebhook(job);
const processed = options.existingOutput
? { outputFile: input, lastResortRepairFile: null }
@@ -1651,7 +1653,7 @@ async function processVideoAsync(
progress: 80,
message: "Uploading processed video...",
});
- await sendWebhook(job);
+ void sendWebhook(job);
// Previews only read the finished output, so render them while it uploads; they are still
// uploaded after the output, so a failed upload never leaves previews behind.
@@ -1692,7 +1694,7 @@ async function processVideoAsync(
progress: 90,
message: "Generating preview assets...",
});
- await sendWebhook(job);
+ void sendWebhook(job);
}
await withJobHeartbeat(jobId, async () => {