Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): name replacement attempts by number so a later step retry still owns them

Richie McIlroy2c653333bc29
diff --git a/apps/web/__tests__/unit/desktop-recording-jobs.test.ts b/apps/web/__tests__/unit/desktop-recording-jobs.test.ts
index 952bda7c1..e725e7f73 100644
--- a/apps/web/__tests__/unit/desktop-recording-jobs.test.ts
+++ b/apps/web/__tests__/unit/desktop-recording-jobs.test.ts
@@ -302,14 +302,19 @@ describe("durable recording job ownership", () => {
expect(rows.jobs?.[0]?.attemptCount).toBe(1);
});
- it("returns a live attempt to the caller that claimed it under the same id", async () => {
+ const stepIds = (attemptCount: number) => `step-attempt-${attemptCount}`;
+
+ it("returns a live attempt to the caller that named it", async () => {
const { job } = await ensureSegmentProcessingJob({ videoId, userId });
const first = await claimProcessingAttempt({
videoId,
generation: job.generation,
- attemptId: "step-attempt",
+ attemptIdFor: stepIds,
+ });
+ expect(first).toMatchObject({
+ attemptId: "step-attempt-1",
+ attemptCount: 1,
});
- expect(first).toMatchObject({ attemptId: "step-attempt", attemptCount: 1 });
const output = {
kind: "desktop-recording-source-commit",
revision: 3,
@@ -318,10 +323,10 @@ describe("durable recording job ownership", () => {
const resumed = await claimProcessingAttempt({
videoId,
generation: job.generation,
- attemptId: "step-attempt",
+ attemptIdFor: stepIds,
});
expect(resumed).toMatchObject({
- attemptId: "step-attempt",
+ attemptId: "step-attempt-1",
attemptCount: 1,
output,
});
@@ -329,7 +334,7 @@ describe("durable recording job ownership", () => {
await claimProcessingAttempt({
videoId,
generation: job.generation,
- attemptId: "other-step",
+ attemptIdFor: (attemptCount) => `other-step-${attemptCount}`,
}),
).toBeNull();
expect(
@@ -338,22 +343,33 @@ describe("durable recording job ownership", () => {
expect(rows.jobs?.[0]?.attemptCount).toBe(1);
});
- it("claims a fresh id when the caller's own attempt has expired", async () => {
+ it("names a replacement for an expired attempt so a later retry still recognises it", async () => {
const { job } = await ensureSegmentProcessingJob({ videoId, userId });
await claimProcessingAttempt({
videoId,
generation: job.generation,
- attemptId: "step-attempt",
+ attemptIdFor: stepIds,
});
vi.setSystemTime(new Date(now.getTime() + 6 * 60_000));
- const next = await claimProcessingAttempt({
+ const replacement = await claimProcessingAttempt({
videoId,
generation: job.generation,
- attemptId: "step-attempt",
+ attemptIdFor: stepIds,
+ });
+ expect(replacement).toMatchObject({
+ attemptId: "step-attempt-2",
+ attemptCount: 2,
+ });
+ const retried = await claimProcessingAttempt({
+ videoId,
+ generation: job.generation,
+ attemptIdFor: stepIds,
+ });
+ expect(retried).toMatchObject({
+ attemptId: "step-attempt-2",
+ attemptCount: 2,
});
- expect(next?.attemptId).toBeTruthy();
- expect(next?.attemptId).not.toBe("step-attempt");
- expect(next?.attemptCount).toBe(2);
+ expect(rows.jobs?.[0]?.attemptCount).toBe(2);
});
it("does not resume a finished attempt under its old id", async () => {
@@ -361,7 +377,7 @@ describe("durable recording job ownership", () => {
const first = await claimProcessingAttempt({
videoId,
generation: job.generation,
- attemptId: "step-attempt",
+ attemptIdFor: stepIds,
});
if (!first) throw new Error("Fixture attempt was not claimed");
Object.assign(rows.jobs?.[0] ?? {}, { state: "verified" });
@@ -369,7 +385,7 @@ describe("durable recording job ownership", () => {
await claimProcessingAttempt({
videoId,
generation: job.generation,
- attemptId: "step-attempt",
+ attemptIdFor: stepIds,
}),
).toBeNull();
});
diff --git a/apps/web/__tests__/unit/finalize-desktop-recording.test.ts b/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
index 0b32b199a..4b203feb0 100644
--- a/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
+++ b/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
@@ -1179,7 +1179,37 @@ describe("source commitment and media request compatibility", () => {
});
describe("first attempt step", () => {
+ // Claims the way the database does: a live attempt named for its number is handed back to its caller.
+ function claimByName() {
+ mocks.claim.mockImplementation(
+ async ({
+ attemptIdFor,
+ }: {
+ attemptIdFor?: (attemptCount: number) => string;
+ }) => {
+ if (!current) return null;
+ if (
+ attemptIdFor &&
+ current.attemptId === attemptIdFor(current.attemptCount) &&
+ current.leaseExpiresAt &&
+ current.leaseExpiresAt > new Date()
+ )
+ return current;
+ const attemptCount = current.attemptCount + 1;
+ withCurrent({
+ state: current.source ? "processing" : "committing",
+ attemptId: attemptIdFor?.(attemptCount) ?? `attempt-${attemptCount}`,
+ attemptCount,
+ leaseExpiresAt: new Date(Date.now() + 5 * 60_000),
+ remoteJobId: null,
+ });
+ return current;
+ },
+ );
+ }
+
it("claims, commits and dispatches the first attempt in one step", async () => {
+ claimByName();
withCurrent({
state: "committing",
source: null,
@@ -1193,68 +1223,48 @@ describe("first attempt step", () => {
});
expect(result).toMatchObject({
status: "attempt",
- started: { committed: "ready", dispatched: true, started: "remote-job" },
- });
- expect(mocks.claim).toHaveBeenCalledWith(
- expect.objectContaining({
+ attempt: {
attemptId: expect.stringMatching(
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
),
- }),
- );
- expect(mocks.fetch).toHaveBeenCalledOnce();
- });
-
- it("derives the same attempt id when the step is retried", async () => {
- withCurrent({ state: "committing", source: null, leaseExpiresAt: null });
- await acquireAndStartDesktopRecordingAttempt({
- videoId,
- userId,
- generation: fixture.generation,
- });
- const first = mocks.claim.mock.calls[0]?.[0].attemptId;
- withCurrent({ attemptId: first });
- await acquireAndStartDesktopRecordingAttempt({
- videoId,
- userId,
- generation: fixture.generation,
+ },
+ started: { committed: "ready", dispatched: true, started: "remote-job" },
});
- expect(mocks.claim.mock.calls[1]?.[0].attemptId).toBe(first);
+ expect(mocks.fetch).toHaveBeenCalledOnce();
});
- it("resumes its own live attempt instead of reporting it as already processing", async () => {
+ it("rethrows an early failure so the step retry resumes the same attempt", async () => {
+ claimByName();
withCurrent({ state: "committing", source: null, leaseExpiresAt: null });
- await acquireAndStartDesktopRecordingAttempt({
- videoId,
- userId,
- generation: fixture.generation,
- });
- const own = mocks.claim.mock.calls[0]?.[0].attemptId;
- withCurrent({
- attemptId: own,
- leaseExpiresAt: new Date(Date.now() + 60_000),
- nextRetryAt: new Date(Date.now() + 60_000),
- });
- mocks.claim.mockImplementationOnce(async () => current);
- const retried = await acquireAndStartDesktopRecordingAttempt({
- videoId,
- userId,
- generation: fixture.generation,
- });
- expect(retried.status).toBe("attempt");
- withCurrent({ attemptId: "someone-else" });
+ mocks.stepMetadata.mockReturnValue({ stepId: "step_retry", attempt: 1 });
+ mocks.commitSource.mockRejectedValueOnce(new Error("storage blip"));
+ await expect(
+ acquireAndStartDesktopRecordingAttempt({
+ videoId,
+ userId,
+ generation: fixture.generation,
+ }),
+ ).rejects.toThrow("storage blip");
+ const claimed = current?.attemptId;
+ mocks.stepMetadata.mockReturnValue({ stepId: "step_retry", attempt: 2 });
expect(
await acquireAndStartDesktopRecordingAttempt({
videoId,
userId,
generation: fixture.generation,
}),
- ).toEqual({ status: "already-processing" });
+ ).toMatchObject({
+ status: "attempt",
+ attempt: { attemptId: claimed, attemptCount: 2 },
+ started: { committed: "ready", dispatched: true, started: "remote-job" },
+ });
+ expect(mocks.fetch).toHaveBeenCalledOnce();
});
- it("rethrows an early failure so the step retry resumes the same attempt", async () => {
+ it("keeps resuming a replacement claimed after its earlier attempt's lease expired", async () => {
+ claimByName();
withCurrent({ state: "committing", source: null, leaseExpiresAt: null });
- mocks.stepMetadata.mockReturnValue({ stepId: "step_retry", attempt: 1 });
+ mocks.stepMetadata.mockReturnValue({ stepId: "step_slow", attempt: 1 });
mocks.commitSource.mockRejectedValueOnce(new Error("storage blip"));
await expect(
acquireAndStartDesktopRecordingAttempt({
@@ -1263,10 +1273,20 @@ describe("first attempt step", () => {
generation: fixture.generation,
}),
).rejects.toThrow("storage blip");
- const claimed = mocks.claim.mock.calls[0]?.[0].attemptId;
- withCurrent({ attemptId: claimed });
- mocks.claim.mockImplementationOnce(async () => current);
- mocks.stepMetadata.mockReturnValue({ stepId: "step_retry", attempt: 2 });
+ const first = current?.attemptId;
+ vi.setSystemTime(new Date(Date.now() + 6 * 60_000));
+ mocks.stepMetadata.mockReturnValue({ stepId: "step_slow", attempt: 2 });
+ mocks.commitSource.mockRejectedValueOnce(new Error("storage blip again"));
+ await expect(
+ acquireAndStartDesktopRecordingAttempt({
+ videoId,
+ userId,
+ generation: fixture.generation,
+ }),
+ ).rejects.toThrow("storage blip again");
+ const replacement = current?.attemptId;
+ expect(replacement).not.toBe(first);
+ mocks.stepMetadata.mockReturnValue({ stepId: "step_slow", attempt: 3 });
expect(
await acquireAndStartDesktopRecordingAttempt({
videoId,
@@ -1275,11 +1295,24 @@ describe("first attempt step", () => {
}),
).toMatchObject({
status: "attempt",
- attempt: { attemptId: claimed },
- started: { committed: "ready", dispatched: true, started: "remote-job" },
+ attempt: { attemptId: replacement },
+ started: { committed: "ready", dispatched: true },
});
- expect(mocks.claim.mock.calls[1]?.[0].attemptId).toBe(claimed);
- expect(mocks.fetch).toHaveBeenCalledOnce();
+ });
+
+ it("leaves another workflow's live attempt alone", async () => {
+ withCurrent({
+ attemptId: "someone-else",
+ leaseExpiresAt: new Date(Date.now() + 60_000),
+ });
+ expect(
+ await acquireAndStartDesktopRecordingAttempt({
+ videoId,
+ userId,
+ generation: fixture.generation,
+ }),
+ ).toEqual({ status: "already-processing" });
+ expect(mocks.claim).not.toHaveBeenCalled();
});
it("returns a failure after the last try as data for the workflow to retry", async () => {
diff --git a/apps/web/lib/desktop-recording-jobs.ts b/apps/web/lib/desktop-recording-jobs.ts
index 2e5568910..2478910fe 100644
--- a/apps/web/lib/desktop-recording-jobs.ts
+++ b/apps/web/lib/desktop-recording-jobs.ts
@@ -434,16 +434,17 @@ async function pauseExhaustedRecording(
.where(eq(videoUploads.videoId, videoId));
}
-// A caller that retries with the same attemptId gets its own live attempt back instead of being locked out by its lease.
+// A caller that names its attempts by number gets its own live attempt back on a retry instead of being
+// locked out by its lease, and its next claim gets the next number's id.
export async function claimProcessingAttempt({
videoId,
generation,
- attemptId,
+ attemptIdFor,
now = new Date(),
}: {
videoId: Video.VideoId;
generation: string;
- attemptId?: string;
+ attemptIdFor?: (attemptCount: number) => string;
now?: Date;
}): Promise<DesktopRecordingAttempt | null> {
return db().transaction(async (tx) => {
@@ -459,16 +460,20 @@ export async function claimProcessingAttempt({
.for("update");
if (!row) return null;
const job = parseDesktopRecordingJob(row);
- const resumed = attemptId !== undefined && job.attemptId === attemptId;
+ const ownAttemptId =
+ attemptIdFor !== undefined &&
+ job.attemptId === attemptIdFor(job.attemptCount)
+ ? job.attemptId
+ : null;
if (
- resumed &&
+ ownAttemptId !== null &&
job.leaseExpiresAt &&
job.leaseExpiresAt > now &&
(job.state === "committing" ||
job.state === "queued" ||
job.state === "processing")
)
- return { ...job, attemptId };
+ return { ...job, attemptId: ownAttemptId };
if (!isDesktopRecordingJobRecoverable(job, now)) return null;
if (job.attemptCount >= MAX_AUTOMATIC_ATTEMPTS) {
await pauseExhaustedRecording(
@@ -482,8 +487,7 @@ export async function claimProcessingAttempt({
const attempt: DesktopRecordingAttempt = {
...job,
state: job.source ? "processing" : "committing",
- // An expired attempt never comes back under its old id, so a retry claims a fresh one.
- attemptId: resumed ? randomUUID() : (attemptId ?? randomUUID()),
+ attemptId: attemptIdFor?.(job.attemptCount + 1) ?? randomUUID(),
attemptCount: job.attemptCount + 1,
leaseExpiresAt: new Date(now.getTime() + DESKTOP_RECORDING_LEASE_MS),
remoteJobId: null,
diff --git a/apps/web/workflows/finalize-desktop-recording.ts b/apps/web/workflows/finalize-desktop-recording.ts
index df62f93dd..f67a6ca84 100644
--- a/apps/web/workflows/finalize-desktop-recording.ts
+++ b/apps/web/workflows/finalize-desktop-recording.ts
@@ -255,12 +255,12 @@ async function acquireAttempt({
videoId,
userId,
generation,
- attemptId,
+ attemptIdFor,
}: {
videoId: string;
userId: User.UserId;
generation: string;
- attemptId?: string;
+ attemptIdFor?: (attemptCount: number) => string;
}): Promise<AttemptAcquisition> {
const id = Video.VideoId.make(videoId);
const job = await getProcessingState({ videoId: id, generation });
@@ -268,16 +268,18 @@ async function acquireAttempt({
if (job.state === "verified") return { status: "verified" };
if (job.state === "source-blocked") return { status: "source-blocked" };
const now = new Date();
- const own = attemptId !== undefined && job.attemptId === attemptId;
- if (!own && job.leaseExpiresAt && job.leaseExpiresAt > now) {
- return { status: "already-processing" };
- }
- if (!own && job.nextRetryAt > now)
+ const live = Boolean(job.leaseExpiresAt && job.leaseExpiresAt > now);
+ const ownLive =
+ live &&
+ attemptIdFor !== undefined &&
+ job.attemptId === attemptIdFor(job.attemptCount);
+ if (live && !ownLive) return { status: "already-processing" };
+ if (!ownLive && job.nextRetryAt > now)
return { status: "wait", until: job.nextRetryAt };
const attempt = await claimProcessingAttempt({
videoId: id,
generation,
- attemptId,
+ attemptIdFor,
now,
});
return attempt
@@ -294,9 +296,12 @@ class AttemptStepFailure extends Error {
}
}
-// A retried step keeps its step id, so an attempt id derived from it lets the retry resume its own claim.
-function stepAttemptId(stepId: string) {
- const digest = createHash("sha256").update(stepId).digest("hex");
+// A retried step keeps its step id, so ids derived from it and the attempt number let the retry recognise
+// its own claim, including a replacement claimed after its earlier attempt's lease expired.
+function stepAttemptId(stepId: string, attemptCount: number) {
+ const digest = createHash("sha256")
+ .update(`${stepId}:${attemptCount}`)
+ .digest("hex");
const variant = (8 + (Number.parseInt(digest.charAt(16), 16) % 4)).toString(
16,
);
@@ -326,9 +331,10 @@ export async function acquireAndStartDesktopRecordingAttempt(input: {
> {
"use step";
+ const { stepId } = getStepMetadata();
const acquired = await acquireAttempt({
...input,
- attemptId: stepAttemptId(getStepMetadata().stepId),
+ attemptIdFor: (attemptCount) => stepAttemptId(stepId, attemptCount),
});
if (acquired.status !== "attempt") return acquired;
try {