Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(web): dispatch a desktop recording in the same step as a quick source commit

Richie McIlroydbd9382a7b21
diff --git a/apps/web/__tests__/unit/finalize-desktop-recording.test.ts b/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
index 0e29db575..0befc5594 100644
--- a/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
+++ b/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
@@ -105,6 +105,7 @@ vi.mock("@cap/web-backend/src/Storage/index", async () => {
});
import {
+ commitAndStartDesktopRecordingAttempt,
commitDesktopRecordingAttempt,
finalizeDesktopRecordingWorkflow,
pollDesktopRecordingAttempt,
@@ -661,6 +662,44 @@ describe("bounded source commitment steps", () => {
}
});
+ it("dispatches in the same step when the commit finishes inside its batch budget", async () => {
+ withCurrent({ source: null });
+ mocks.commitSource.mockResolvedValue({ source });
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({
+ committed: "ready",
+ dispatched: true,
+ started: "remote-job",
+ });
+ expect(mocks.persist).toHaveBeenCalledOnce();
+ expect(mocks.fetch).toHaveBeenCalledOnce();
+ });
+
+ it("leaves dispatch to its own step after a slow commit", async () => {
+ withCurrent({ source: null });
+ mocks.commitSource.mockImplementation(async () => {
+ vi.setSystemTime(Date.now() + 15_000);
+ return { source };
+ });
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({ committed: "ready" });
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ });
+
+ it("does not dispatch while the source commit is still in progress", async () => {
+ withCurrent({ source: null });
+ mocks.commitSource.mockImplementation(async (_video, checkpoint) => {
+ vi.setSystemTime(Date.now() + 15_000);
+ return advanceCheckpoint(_video, checkpoint);
+ });
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({ committed: "progress" });
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ });
+
it("includes checkpoint persistence latency in the batch budget", async () => {
mocks.commitSource.mockImplementation(advanceCheckpoint);
mocks.saveCheckpoint.mockImplementation(async (_attempt, checkpoint) => {
diff --git a/apps/web/workflows/finalize-desktop-recording.ts b/apps/web/workflows/finalize-desktop-recording.ts
index cafe427ac..cc8323318 100644
--- a/apps/web/workflows/finalize-desktop-recording.ts
+++ b/apps/web/workflows/finalize-desktop-recording.ts
@@ -143,14 +143,16 @@ export async function finalizeDesktopRecordingWorkflow(
}
const attempt = next.attempt;
try {
- let committed = await commitDesktopRecordingAttempt(attempt);
- while (committed === "progress") {
- committed = await commitDesktopRecordingAttempt(attempt);
+ let committed = await commitAndStartDesktopRecordingAttempt(attempt);
+ while (committed.committed === "progress") {
+ committed = await commitAndStartDesktopRecordingAttempt(attempt);
}
- if (committed !== "ready") {
- return { success: false, reason: committed };
+ if (committed.committed !== "ready") {
+ return { success: false, reason: committed.committed };
}
- const started = await startDesktopRecordingJob(attempt);
+ const started = committed.dispatched
+ ? committed.started
+ : await startDesktopRecordingJob(attempt);
if (typeof started === "object") {
if (started.status === "media-server-unconfigured") {
mediaServerUnavailable = true;
@@ -276,6 +278,36 @@ export async function commitDesktopRecordingAttempt(
): Promise<SourceCommitResult> {
"use step";
+ return commitDesktopRecordingBatch(attempt);
+}
+
+// Each step costs a queue hop, so a commit that finishes inside its batch budget dispatches in the same step.
+// A slow commit returns without dispatching and the workflow runs startDesktopRecordingJob as its own step.
+export async function commitAndStartDesktopRecordingAttempt(
+ attempt: DesktopRecordingAttempt,
+): Promise<
+ | { committed: SourceCommitResult; dispatched?: undefined }
+ | { committed: "ready"; dispatched: true; started: DesktopRecordingDispatch }
+> {
+ "use step";
+
+ const startedAt = Date.now();
+ const committed = await commitDesktopRecordingBatch(attempt);
+ if (
+ committed !== "ready" ||
+ Date.now() - startedAt >= SOURCE_COMMIT_BATCH_DURATION_MS
+ )
+ return { committed };
+ return {
+ committed,
+ dispatched: true,
+ started: await dispatchDesktopRecordingJob(attempt),
+ };
+}
+
+async function commitDesktopRecordingBatch(
+ attempt: DesktopRecordingAttempt,
+): Promise<SourceCommitResult> {
const startedAt = Date.now();
for (let index = 0; index < SOURCE_COMMIT_BATCH_MAX_CHECKPOINTS; index++) {
try {
@@ -480,17 +512,24 @@ async function buildDesktopSegmentsOutput({
};
}
-export async function startDesktopRecordingJob(
- attempt: DesktopRecordingAttempt,
-): Promise<
+type DesktopRecordingDispatch =
| string
| undefined
| { status: "deferred" }
| { status: "capacity"; retryAfterMs: number }
- | { status: "media-server-unconfigured" }
-> {
+ | { status: "media-server-unconfigured" };
+
+export async function startDesktopRecordingJob(
+ attempt: DesktopRecordingAttempt,
+): Promise<DesktopRecordingDispatch> {
"use step";
+ return dispatchDesktopRecordingJob(attempt);
+}
+
+async function dispatchDesktopRecordingJob(
+ attempt: DesktopRecordingAttempt,
+): Promise<DesktopRecordingDispatch> {
if (await completeWithoutMediaServerIfUnavailable(attempt)) {
return { status: "media-server-unconfigured" };
}