Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf: create a desktop recording's output targets together and upload its thumbnail and preview GIF in parallel

Richie McIlroy293819b7d41e
diff --git a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
index f957ac607..40339e066 100644
--- a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
@@ -43,6 +43,7 @@ let tempDir = "";
const uploadedArtifacts = new Map<string, Uint8Array>();
const uploadFailures = new Map<string, number>();
+const uploadGates = new Map<string, Promise<void>>();
const uploadRequests: string[] = [];
const fixtureReads: string[] = [];
const webhookPhases: JobProgress["phase"][] = [];
@@ -442,6 +443,7 @@ beforeAll(async () => {
if (request.method === "PUT" && url.pathname.startsWith("/uploads/")) {
uploadRequests.push(url.pathname);
+ await uploadGates.get(url.pathname);
const failureStatus = uploadFailures.get(url.pathname);
if (failureStatus) {
return new Response("Storage unavailable", { status: failureStatus });
@@ -490,6 +492,7 @@ beforeEach(() => {
});
uploadedArtifacts.clear();
uploadFailures.clear();
+ uploadGates.clear();
uploadRequests.length = 0;
fixtureReads.length = 0;
webhookPhases.length = 0;
@@ -718,6 +721,54 @@ describe("media routes real-world integration tests", () => {
}
}, 30_000);
+ test("uploads the thumbnail and preview GIF together and keeps one when the other fails", async () => {
+ let gifStarted: () => void = () => {};
+ const gifRequested = new Promise<void>((resolve) => {
+ gifStarted = resolve;
+ });
+ uploadGates.set(
+ "/uploads/mux-previews-together.jpg",
+ Promise.race([
+ gifRequested,
+ new Promise<void>((resolve) => setTimeout(resolve, 5_000)),
+ ]),
+ );
+ uploadGates.set(
+ "/uploads/mux-previews-together.gif",
+ Promise.resolve().then(gifStarted),
+ );
+ uploadFailures.set("/uploads/mux-previews-together.jpg", 503);
+ const body = {
+ ...fencedMuxRequest("mux-previews-together"),
+ thumbnailPresignedUrl: uploadUrl("mux-previews-together.jpg"),
+ previewGifPresignedUrl: uploadUrl("mux-previews-together.gif"),
+ };
+ const response = await app.fetch(
+ mediaPostRequest("/video/mux-segments", body),
+ );
+ expect(response.status).toBe(200);
+ const { jobId } = (await response.json()) as { jobId: string };
+ const started = Date.now();
+ try {
+ const job = await waitForTerminalJob(jobId);
+ expect(job.phase).toBe("complete");
+ expect(uploadRequests[0]).toBe("/uploads/mux-previews-together.mp4");
+ expect(new Set(uploadRequests.slice(1, 3))).toEqual(
+ new Set([
+ "/uploads/mux-previews-together.jpg",
+ "/uploads/mux-previews-together.gif",
+ ]),
+ );
+ // The thumbnail is held until the GIF upload starts, so a sequential upload would wait out the 5 s gate.
+ expect(Date.now() - started).toBeLessThan(4_500);
+ expect(
+ uploadedBytes("/uploads/mux-previews-together.gif").byteLength,
+ ).toBeGreaterThan(0);
+ } finally {
+ deleteJob(jobId);
+ }
+ }, 30_000);
+
test("skips mux previews when the recording output upload fails", async () => {
uploadFailures.set("/uploads/mux-previews-failed.mp4", 403);
const body = {
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index 32b8e5b9f..0bb5ae53c 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -3381,38 +3381,36 @@ async function muxSegmentsAsync(
await withJobHeartbeat(jobId, async () => {
const assets = await previewAssets;
abortController.signal.throwIfAborted();
- if (thumbnailPresignedUrl && assets?.thumbnail) {
- try {
- await uploadToS3(
- assets.thumbnail,
- thumbnailPresignedUrl,
- "image/jpeg",
- abortController.signal,
- );
- } catch (thumbErr) {
- abortController.signal.throwIfAborted();
- console.warn(
- `[mux-segments] Thumbnail generation failed for ${videoId}:`,
- thumbErr,
- );
- }
- }
- if (previewGifPresignedUrl && assets?.previewGif) {
- try {
- await uploadFileToS3(
- assets.previewGif.path,
- previewGifPresignedUrl,
- "image/gif",
- abortController.signal,
- );
- } catch (previewErr) {
- abortController.signal.throwIfAborted();
- console.warn(
- "[mux-segments] Preview GIF generation failed:",
- previewErr,
- );
- }
- }
+ await Promise.all([
+ thumbnailPresignedUrl && assets?.thumbnail
+ ? uploadToS3(
+ assets.thumbnail,
+ thumbnailPresignedUrl,
+ "image/jpeg",
+ abortController.signal,
+ ).catch((thumbErr) => {
+ abortController.signal.throwIfAborted();
+ console.warn(
+ `[mux-segments] Thumbnail generation failed for ${videoId}:`,
+ thumbErr,
+ );
+ })
+ : undefined,
+ previewGifPresignedUrl && assets?.previewGif
+ ? uploadFileToS3(
+ assets.previewGif.path,
+ previewGifPresignedUrl,
+ "image/gif",
+ abortController.signal,
+ ).catch((previewErr) => {
+ abortController.signal.throwIfAborted();
+ console.warn(
+ "[mux-segments] Preview GIF generation failed:",
+ previewErr,
+ );
+ })
+ : undefined,
+ ]);
});
abortController.signal.throwIfAborted();
diff --git a/apps/web/workflows/finalize-desktop-recording.ts b/apps/web/workflows/finalize-desktop-recording.ts
index 1cf017d7d..aff95076d 100644
--- a/apps/web/workflows/finalize-desktop-recording.ts
+++ b/apps/web/workflows/finalize-desktop-recording.ts
@@ -437,18 +437,43 @@ async function buildDesktopSegmentsOutput({
const env = serverEnv();
const webhookBaseUrl = env.MEDIA_SERVER_WEBHOOK_URL || env.WEB_URL;
const webhookSecret = env.MEDIA_SERVER_WEBHOOK_SECRET;
- const outputVerificationUrl = await bucket
- .getInternalSignedObjectUrl(outputKey, {
- expiresIn: PRESIGNED_EXPIRES_SECONDS,
- })
- .pipe(runWorkflowPromise);
- const outputPresignedUrl = await bucket
- .getInternalPresignedPutUrl(
- outputKey,
- { ContentType: "video/mp4" },
- { expiresIn: PRESIGNED_EXPIRES_SECONDS },
- )
- .pipe(runWorkflowPromise);
+ // Each Google Drive target is a resumable session costing two Drive round trips, so create them together.
+ const [
+ outputVerificationUrl,
+ outputPresignedUrl,
+ thumbnailPresignedUrl,
+ previewGifPresignedUrl,
+ ] = await Promise.all([
+ bucket
+ .getInternalSignedObjectUrl(outputKey, {
+ expiresIn: PRESIGNED_EXPIRES_SECONDS,
+ })
+ .pipe(runWorkflowPromise),
+ bucket
+ .getInternalPresignedPutUrl(
+ outputKey,
+ { ContentType: "video/mp4" },
+ { expiresIn: PRESIGNED_EXPIRES_SECONDS },
+ )
+ .pipe(runWorkflowPromise),
+ bucket
+ .getInternalPresignedPutUrl(
+ `${candidateDirectory}/screenshot.jpg`,
+ { ContentType: "image/jpeg" },
+ { expiresIn: PRESIGNED_EXPIRES_SECONDS },
+ )
+ .pipe(runWorkflowPromise),
+ bucket
+ .getInternalPresignedPutUrl(
+ `${candidateDirectory}/preview.gif`,
+ {
+ ContentType: "image/gif",
+ CacheControl: "public, max-age=31536000, immutable",
+ },
+ { expiresIn: PRESIGNED_EXPIRES_SECONDS },
+ )
+ .pipe(runWorkflowPromise),
+ ]);
let outputUpload: DesktopSegmentsOutputUpload = {
type: "put",
url: outputPresignedUrl,
@@ -487,23 +512,6 @@ async function buildDesktopSegmentsOutput({
webhookSecret,
};
}
- const thumbnailPresignedUrl = await bucket
- .getInternalPresignedPutUrl(
- `${candidateDirectory}/screenshot.jpg`,
- { ContentType: "image/jpeg" },
- { expiresIn: PRESIGNED_EXPIRES_SECONDS },
- )
- .pipe(runWorkflowPromise);
- const previewGifPresignedUrl = await bucket
- .getInternalPresignedPutUrl(
- `${candidateDirectory}/preview.gif`,
- {
- ContentType: "image/gif",
- CacheControl: "public, max-age=31536000, immutable",
- },
- { expiresIn: PRESIGNED_EXPIRES_SECONDS },
- )
- .pipe(runWorkflowPromise);
return {
outputKey,
outputVerificationUrl,