Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): let an admin retry blocked desktop recordings that already have a saved source

Richie McIlroy30d778095871
diff --git a/apps/web/__tests__/unit/admin-retry-blocked-recordings.test.ts b/apps/web/__tests__/unit/admin-retry-blocked-recordings.test.ts
new file mode 100644
index 000000000..21fe24955
--- /dev/null
+++ b/apps/web/__tests__/unit/admin-retry-blocked-recordings.test.ts
@@ -0,0 +1,101 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ user: vi.fn(),
+ retry: vi.fn(),
+ queue: vi.fn(),
+}));
+vi.mock("@cap/database/auth/session", () => ({ getCurrentUser: mocks.user }));
+vi.mock("@cap/env", () => ({
+ serverEnv: () => ({ WEB_URL: "https://cap.test" }),
+}));
+vi.mock("@/lib/messenger/constants", () => ({
+ MESSENGER_ADMIN_EMAIL: "[email protected]",
+}));
+vi.mock("@/lib/desktop-recording-jobs", () => ({
+ retryBlockedDesktopRecording: mocks.retry,
+}));
+vi.mock("@/lib/desktop-segments-finalization", () => ({
+ queueDesktopSegmentsFinalization: mocks.queue,
+}));
+
+import { adminRetryBlockedRecordings } from "@/actions/admin/retry-blocked-recordings";
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.user.mockResolvedValue({ email: "[email protected]" });
+ mocks.queue.mockResolvedValue("queued");
+});
+
+describe("admin retry of blocked recordings", () => {
+ it("rejects anyone who is not the admin", async () => {
+ mocks.user.mockResolvedValue({ email: "[email protected]" });
+ await expect(adminRetryBlockedRecordings("video1")).rejects.toThrow(
+ "Unauthorized",
+ );
+ expect(mocks.retry).not.toHaveBeenCalled();
+ });
+
+ it("limits a request to 25 recordings", async () => {
+ const input = Array.from({ length: 26 }, (_, i) => `video${i}`).join("\n");
+ await expect(adminRetryBlockedRecordings(input)).rejects.toThrow(
+ "at most 25",
+ );
+ expect(mocks.retry).not.toHaveBeenCalled();
+ });
+
+ it("queues retryable recordings once each and reports the rest without starting them", async () => {
+ mocks.retry.mockImplementation(async ({ videoId }: { videoId: string }) =>
+ videoId === "blocked1"
+ ? {
+ status: "queued",
+ ownerId: "owner1",
+ previousErrorCode: "processing-budget-exhausted",
+ }
+ : videoId === "missing1"
+ ? { status: "not-found" }
+ : { status: "not-retryable", state: "verified", errorCode: null },
+ );
+ const results = await adminRetryBlockedRecordings(
+ "https://cap.so/s/blocked1\nmissing1, verified1 blocked1",
+ );
+ expect(results).toEqual([
+ {
+ input: "https://cap.so/s/blocked1",
+ videoId: "blocked1",
+ status: "queued",
+ detail: "was processing-budget-exhausted; queued",
+ shareUrl: "https://cap.test/s/blocked1",
+ },
+ { input: "missing1", videoId: "missing1", status: "not-found" },
+ {
+ input: "verified1",
+ videoId: "verified1",
+ status: "not-retryable",
+ detail: "verified",
+ },
+ ]);
+ expect(mocks.retry).toHaveBeenCalledTimes(3);
+ expect(mocks.queue).toHaveBeenCalledTimes(1);
+ expect(mocks.queue).toHaveBeenCalledWith({
+ videoId: "blocked1",
+ userId: "owner1",
+ });
+ });
+
+ it("reports a failed workflow start instead of throwing", async () => {
+ mocks.retry.mockResolvedValue({
+ status: "queued",
+ ownerId: "owner1",
+ previousErrorCode: "source-invalid",
+ });
+ mocks.queue.mockRejectedValue(new Error("Workflow dispatch failed"));
+ expect(await adminRetryBlockedRecordings("blocked1")).toEqual([
+ {
+ input: "blocked1",
+ status: "failed",
+ detail: "Workflow dispatch failed",
+ },
+ ]);
+ });
+});
diff --git a/apps/web/__tests__/unit/desktop-recording-jobs.test.ts b/apps/web/__tests__/unit/desktop-recording-jobs.test.ts
index 0d596a369..42f44244e 100644
--- a/apps/web/__tests__/unit/desktop-recording-jobs.test.ts
+++ b/apps/web/__tests__/unit/desktop-recording-jobs.test.ts
@@ -16,6 +16,7 @@ import {
persistCommittedSource,
persistSourceCommitCheckpoint,
retireDesktopRecordingJobForOutputReplacement,
+ retryBlockedDesktopRecording,
scheduleRetry,
waitForDesktopRecordingCapacity,
} from "@/lib/desktop-recording-jobs";
@@ -35,6 +36,7 @@ vi.mock("@cap/database/schema", () => {
return {
videos: table("videos", ["id", "ownerId", "source"]),
videoUploads: table("uploads", ["videoId"]),
+ mediaProcessingBudgets: table("budgets", ["id"]),
videoProcessingJobs: table("jobs", [
"videoId",
"ownerId",
@@ -173,6 +175,17 @@ function createClient() {
},
};
},
+ delete(table: Table) {
+ return {
+ where: async (condition: Condition) => {
+ const before = rows[table.table] ?? [];
+ rows[table.table] = before.filter((row) => !matches(row, condition));
+ return [
+ { affectedRows: before.length - (rows[table.table]?.length ?? 0) },
+ ];
+ },
+ };
+ },
insert(table: Table) {
return {
values(values: Row) {
@@ -236,6 +249,7 @@ beforeEach(() => {
],
jobs: [],
uploads: [],
+ budgets: [],
};
mocks.db.mockReturnValue(createClient());
});
@@ -610,6 +624,95 @@ describe("late verification and source commitment", () => {
});
});
+describe("operator retry of blocked recordings", () => {
+ async function blockedWithSource(errorCode: string) {
+ const attempt = await createAttempt();
+ await persistCommittedSource(attempt, source);
+ Object.assign(getJobRow(), {
+ state: "source-blocked",
+ leaseExpiresAt: null,
+ nextRetryAt: new Date(now.getTime() + 3_600_000),
+ attemptCount: 4,
+ errorCode,
+ errorMessage: "earlier failure",
+ });
+ rows.uploads = [{ videoId, phase: "error", processingError: "earlier" }];
+ const { recordingBudgetKey } = await import(
+ "@/lib/media-processing-budget"
+ );
+ rows.budgets = [
+ {
+ id: recordingBudgetKey(videoId, attempt.generation),
+ reservedBytes: 900,
+ },
+ { id: recordingBudgetKey("other-video", "other"), reservedBytes: 1 },
+ ];
+ return attempt;
+ }
+
+ it.each([
+ "processing-budget-exhausted",
+ "source-invalid",
+ "processing-retry-exhausted",
+ ])(
+ "releases a %s recording with a committed source back into normal processing",
+ async (errorCode) => {
+ const attempt = await blockedWithSource(errorCode);
+ expect(await retryBlockedDesktopRecording({ videoId })).toEqual({
+ status: "queued",
+ ownerId: userId,
+ previousErrorCode: errorCode,
+ });
+ expect(getJobRow()).toMatchObject({
+ generation: attempt.generation,
+ state: "retry",
+ source,
+ attemptCount: 0,
+ leaseExpiresAt: null,
+ nextRetryAt: now,
+ errorCode: null,
+ errorMessage: null,
+ });
+ expect(rows.budgets?.map((row) => row.reservedBytes)).toEqual([1]);
+ expect(rows.uploads?.[0]).toMatchObject({
+ phase: "processing",
+ processingError: null,
+ });
+ const next = await claimProcessingAttempt({
+ videoId,
+ generation: attempt.generation,
+ });
+ expect(next).toMatchObject({
+ state: "processing",
+ attemptCount: 1,
+ source,
+ });
+ },
+ );
+
+ it.each([
+ ["a recording without a committed source", { source: null }],
+ ["an intentionally replaced output", { errorCode: "output-replaced" }],
+ ["a recording being deleted", { errorCode: "video-deleting" }],
+ ["a verified recording", { state: "verified", errorCode: null }],
+ ["a recording still processing", { state: "processing", errorCode: null }],
+ ])("leaves %s untouched", async (_name, change) => {
+ await blockedWithSource("processing-budget-exhausted");
+ Object.assign(getJobRow(), change);
+ const before = structuredClone(rows);
+ expect(await retryBlockedDesktopRecording({ videoId })).toMatchObject({
+ status: "not-retryable",
+ });
+ expect(rows).toEqual(before);
+ });
+
+ it("reports a missing job", async () => {
+ expect(await retryBlockedDesktopRecording({ videoId })).toEqual({
+ status: "not-found",
+ });
+ });
+});
+
describe("retained-source retry policy", () => {
it("persists capacity waiting while retaining the attempt and extending its lease past backoff", async () => {
const attempt = await createAttempt();
diff --git a/apps/web/actions/admin/reprocess-video.ts b/apps/web/actions/admin/reprocess-video.ts
index 8f7d087cf..cfd2cd98e 100644
--- a/apps/web/actions/admin/reprocess-video.ts
+++ b/apps/web/actions/admin/reprocess-video.ts
@@ -1,47 +1,16 @@
"use server";
import { db } from "@cap/database";
-import { getCurrentUser } from "@cap/database/auth/session";
import { videos, videoUploads } from "@cap/database/schema";
import { serverEnv } from "@cap/env";
-import { Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { start } from "workflow/api";
-import { MESSENGER_ADMIN_EMAIL } from "@/lib/messenger/constants";
+import { parseAdminVideoId, requireAdmin } from "@/lib/admin-access";
import { adminReprocessVideoWorkflow } from "@/workflows/admin-reprocess-video";
-async function requireAdmin() {
- const user = await getCurrentUser();
- if (!user || user.email !== MESSENGER_ADMIN_EMAIL) {
- throw new Error("Unauthorized");
- }
- return user;
-}
-
-function parseVideoId(input: string) {
- const trimmed = input.trim();
- if (!trimmed) {
- throw new Error("Enter a video ID or share URL");
- }
-
- try {
- const url = new URL(trimmed);
- const pathMatch = url.pathname.match(/\/s\/([^/?#]+)/);
- if (pathMatch?.[1]) {
- return Video.VideoId.make(pathMatch[1]);
- }
- const lastSegment = url.pathname.split("/").filter(Boolean).at(-1);
- if (lastSegment) {
- return Video.VideoId.make(lastSegment);
- }
- } catch {}
-
- return Video.VideoId.make(trimmed);
-}
-
export async function adminReprocessVideo(input: string) {
await requireAdmin();
- const videoId = parseVideoId(input);
+ const videoId = parseAdminVideoId(input);
const [video] = await db()
.select({
diff --git a/apps/web/actions/admin/retry-blocked-recordings.ts b/apps/web/actions/admin/retry-blocked-recordings.ts
new file mode 100644
index 000000000..0112f9910
--- /dev/null
+++ b/apps/web/actions/admin/retry-blocked-recordings.ts
@@ -0,0 +1,77 @@
+"use server";
+
+import { serverEnv } from "@cap/env";
+import { parseAdminVideoId, requireAdmin } from "@/lib/admin-access";
+import { retryBlockedDesktopRecording } from "@/lib/desktop-recording-jobs";
+import { queueDesktopSegmentsFinalization } from "@/lib/desktop-segments-finalization";
+
+const MAX_RECORDINGS_PER_REQUEST = 25;
+
+export type RetryBlockedRecordingResult = {
+ input: string;
+ videoId?: string;
+ status: "queued" | "not-found" | "not-retryable" | "failed";
+ detail?: string;
+ shareUrl?: string;
+};
+
+export async function adminRetryBlockedRecordings(
+ input: string,
+): Promise<RetryBlockedRecordingResult[]> {
+ await requireAdmin();
+ const entries = [
+ ...new Set(
+ input
+ .split(/[\s,]+/)
+ .map((entry) => entry.trim())
+ .filter(Boolean),
+ ),
+ ];
+ if (entries.length === 0) throw new Error("Enter at least one video ID");
+ if (entries.length > MAX_RECORDINGS_PER_REQUEST)
+ throw new Error(
+ `Retry at most ${MAX_RECORDINGS_PER_REQUEST} recordings at a time`,
+ );
+
+ const results: RetryBlockedRecordingResult[] = [];
+ const seen = new Set<string>();
+ for (const entry of entries) {
+ try {
+ const videoId = parseAdminVideoId(entry);
+ if (seen.has(videoId)) continue;
+ seen.add(videoId);
+ const retried = await retryBlockedDesktopRecording({ videoId });
+ if (retried.status === "not-found") {
+ results.push({ input: entry, videoId, status: "not-found" });
+ continue;
+ }
+ if (retried.status === "not-retryable") {
+ results.push({
+ input: entry,
+ videoId,
+ status: "not-retryable",
+ detail: `${retried.state}${retried.errorCode ? ` (${retried.errorCode})` : ""}`,
+ });
+ continue;
+ }
+ const queued = await queueDesktopSegmentsFinalization({
+ videoId,
+ userId: retried.ownerId,
+ });
+ results.push({
+ input: entry,
+ videoId,
+ status: "queued",
+ detail: `was ${retried.previousErrorCode}; ${queued}`,
+ shareUrl: `${serverEnv().WEB_URL}/s/${videoId}`,
+ });
+ } catch (error) {
+ results.push({
+ input: entry,
+ status: "failed",
+ detail: error instanceof Error ? error.message : String(error),
+ });
+ }
+ }
+ return results;
+}
diff --git a/apps/web/app/admin/AdminPanel.tsx b/apps/web/app/admin/AdminPanel.tsx
index a482cb131..bff610345 100644
--- a/apps/web/app/admin/AdminPanel.tsx
+++ b/apps/web/app/admin/AdminPanel.tsx
@@ -595,6 +595,12 @@ export function AdminPanel({
>
Replace video
</Link>
+ <Link
+ href="/admin/retry-recordings"
+ className="rounded-lg border border-gray-300 px-3 py-2 text-sm font-medium text-gray-700 transition hover:bg-gray-50"
+ >
+ Retry blocked recordings
+ </Link>
</div>
</div>
</div>
diff --git a/apps/web/app/admin/retry-recordings/RetryRecordingsPanel.tsx b/apps/web/app/admin/retry-recordings/RetryRecordingsPanel.tsx
new file mode 100644
index 000000000..c9eff7623
--- /dev/null
+++ b/apps/web/app/admin/retry-recordings/RetryRecordingsPanel.tsx
@@ -0,0 +1,122 @@
+"use client";
+
+import Link from "next/link";
+import { useCallback, useId, useState } from "react";
+import {
+ adminRetryBlockedRecordings,
+ type RetryBlockedRecordingResult,
+} from "@/actions/admin/retry-blocked-recordings";
+
+type Status =
+ | { type: "idle" }
+ | { type: "submitting" }
+ | { type: "done"; results: RetryBlockedRecordingResult[] }
+ | { type: "error"; message: string };
+
+export function RetryRecordingsPanel() {
+ const [input, setInput] = useState("");
+ const [status, setStatus] = useState<Status>({ type: "idle" });
+ const inputId = useId();
+
+ const handleSubmit = useCallback(
+ async (e: React.FormEvent) => {
+ e.preventDefault();
+ if (!input.trim() || status.type === "submitting") return;
+ setStatus({ type: "submitting" });
+ try {
+ setStatus({
+ type: "done",
+ results: await adminRetryBlockedRecordings(input),
+ });
+ } catch (err) {
+ setStatus({
+ type: "error",
+ message: err instanceof Error ? err.message : "Something went wrong",
+ });
+ }
+ },
+ [input, status.type],
+ );
+
+ return (
+ <div className="mx-auto w-full max-w-xl px-5 py-8 md:px-8 md:py-10">
+ <div className="mb-6">
+ <Link
+ href="/admin"
+ className="mb-4 inline-flex text-sm font-medium text-gray-500 transition hover:text-gray-900"
+ >
+ Back to admin
+ </Link>
+ <h1 className="text-2xl font-semibold tracking-tight text-gray-900">
+ Retry blocked recordings
+ </h1>
+ <p className="mt-1 text-sm text-gray-500">
+ Re-run processing for desktop recordings whose source is saved but
+ processing stopped (transfer budget used up, verification failed, or
+ attempt limit reached). Up to 25 at a time.
+ </p>
+ </div>
+
+ <form onSubmit={handleSubmit} className="space-y-5">
+ <div>
+ <label
+ htmlFor={inputId}
+ className="mb-1.5 block text-sm font-medium text-gray-700"
+ >
+ Video IDs or share URLs
+ </label>
+ <textarea
+ id={inputId}
+ value={input}
+ onChange={(e) => setInput(e.target.value)}
+ rows={6}
+ placeholder="One per line"
+ className="w-full rounded-lg border border-gray-300 px-3.5 py-2.5 text-sm text-gray-900 outline-none transition placeholder:text-gray-400 hover:border-gray-400 focus:border-blue-500 focus:ring-2 focus:ring-blue-100"
+ />
+ </div>
+ <button
+ type="submit"
+ disabled={!input.trim() || status.type === "submitting"}
+ className="w-full rounded-lg bg-gray-900 px-4 py-2.5 text-sm font-medium text-white transition hover:bg-gray-800 disabled:opacity-40"
+ >
+ {status.type === "submitting" ? "Retrying..." : "Retry processing"}
+ </button>
+ </form>
+
+ {status.type === "done" && (
+ <ul className="mt-4 space-y-2 text-sm">
+ {status.results.map((result) => (
+ <li
+ key={result.input}
+ className={`rounded-lg border px-4 py-2 ${
+ result.status === "queued"
+ ? "border-emerald-200 bg-emerald-50 text-emerald-700"
+ : "border-amber-200 bg-amber-50 text-amber-800"
+ }`}
+ >
+ <span className="font-medium">
+ {result.videoId ?? result.input}
+ </span>
+ : {result.status}
+ {result.detail ? ` (${result.detail})` : ""}
+ {result.shareUrl && (
+ <Link
+ href={result.shareUrl}
+ className="ml-2 underline underline-offset-2"
+ >
+ Open
+ </Link>
+ )}
+ </li>
+ ))}
+ </ul>
+ )}
+
+ {status.type === "error" && (
+ <div className="mt-4 rounded-lg border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700">
+ {status.message}
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/apps/web/app/admin/retry-recordings/page.tsx b/apps/web/app/admin/retry-recordings/page.tsx
new file mode 100644
index 000000000..03aead2da
--- /dev/null
+++ b/apps/web/app/admin/retry-recordings/page.tsx
@@ -0,0 +1,13 @@
+import { buildEnv } from "@cap/env";
+import { notFound } from "next/navigation";
+import { getViewerContext } from "@/lib/messenger/data";
+import { RetryRecordingsPanel } from "./RetryRecordingsPanel";
+
+export default async function RetryRecordingsPage() {
+ if (buildEnv.NEXT_PUBLIC_IS_CAP !== "true") notFound();
+
+ const viewer = await getViewerContext();
+ if (!viewer.user || !viewer.isAdmin) notFound();
+
+ return <RetryRecordingsPanel />;
+}
diff --git a/apps/web/lib/admin-access.ts b/apps/web/lib/admin-access.ts
new file mode 100644
index 000000000..07d4ffb84
--- /dev/null
+++ b/apps/web/lib/admin-access.ts
@@ -0,0 +1,32 @@
+import { getCurrentUser } from "@cap/database/auth/session";
+import { Video } from "@cap/web-domain";
+import { MESSENGER_ADMIN_EMAIL } from "@/lib/messenger/constants";
+
+export async function requireAdmin() {
+ const user = await getCurrentUser();
+ if (!user || user.email !== MESSENGER_ADMIN_EMAIL) {
+ throw new Error("Unauthorized");
+ }
+ return user;
+}
+
+export function parseAdminVideoId(input: string) {
+ const trimmed = input.trim();
+ if (!trimmed) {
+ throw new Error("Enter a video ID or share URL");
+ }
+
+ try {
+ const url = new URL(trimmed);
+ const pathMatch = url.pathname.match(/\/s\/([^/?#]+)/);
+ if (pathMatch?.[1]) {
+ return Video.VideoId.make(pathMatch[1]);
+ }
+ const lastSegment = url.pathname.split("/").filter(Boolean).at(-1);
+ if (lastSegment) {
+ return Video.VideoId.make(lastSegment);
+ }
+ } catch {}
+
+ return Video.VideoId.make(trimmed);
+}
diff --git a/apps/web/lib/desktop-recording-jobs.ts b/apps/web/lib/desktop-recording-jobs.ts
index b0228443f..434a6d175 100644
--- a/apps/web/lib/desktop-recording-jobs.ts
+++ b/apps/web/lib/desktop-recording-jobs.ts
@@ -1,6 +1,7 @@
import { randomUUID } from "node:crypto";
import { db } from "@cap/database";
import {
+ mediaProcessingBudgets,
videoProcessingJobs,
videos,
videoUploads,
@@ -28,6 +29,7 @@ import {
type RecordingVerification,
recordingVerificationSchema,
} from "@/lib/desktop-recording-verification";
+import { recordingBudgetKey } from "@/lib/media-processing-budget";
export const DESKTOP_RECORDING_LEASE_MS = 5 * 60 * 1_000;
export const DESKTOP_RECORDING_SOURCE_RETRY_MS = 60 * 60 * 1_000;
@@ -931,6 +933,89 @@ export async function deferWithoutMediaServer(
});
}
+// Blocked recordings whose source is committed and whose failure an operator can reasonably retry once the cause is fixed.
+const OPERATOR_RETRYABLE_BLOCKS = new Set([
+ "processing-budget-exhausted",
+ "source-invalid",
+ DESKTOP_RECORDING_RETRY_EXHAUSTED,
+]);
+
+export async function retryBlockedDesktopRecording({
+ videoId,
+ now = new Date(),
+}: {
+ videoId: Video.VideoId;
+ now?: Date;
+}): Promise<
+ | { status: "queued"; ownerId: User.UserId; previousErrorCode: string }
+ | { status: "not-found" }
+ | { status: "not-retryable"; state: string; errorCode: string | null }
+> {
+ return db().transaction(async (tx) => {
+ const [row] = await tx
+ .select()
+ .from(videoProcessingJobs)
+ .where(eq(videoProcessingJobs.videoId, videoId))
+ .for("update");
+ if (!row) return { status: "not-found" as const };
+ const job = parseDesktopRecordingJob(row);
+ if (
+ job.state !== "source-blocked" ||
+ !job.source ||
+ !job.errorCode ||
+ !OPERATOR_RETRYABLE_BLOCKS.has(job.errorCode)
+ )
+ return {
+ status: "not-retryable" as const,
+ state: job.state,
+ errorCode: job.errorCode,
+ };
+ await tx
+ .update(videoProcessingJobs)
+ .set({
+ state: "retry",
+ attemptCount: 0,
+ leaseExpiresAt: null,
+ nextRetryAt: now,
+ workflowRunId: null,
+ errorCode: null,
+ errorMessage: null,
+ updatedAt: now,
+ })
+ .where(
+ and(
+ eq(videoProcessingJobs.videoId, videoId),
+ eq(videoProcessingJobs.generation, job.generation),
+ eq(videoProcessingJobs.state, "source-blocked"),
+ ),
+ );
+ // Earlier attempts spent this generation's transfer budget; the retry starts with a fresh one.
+ await tx
+ .delete(mediaProcessingBudgets)
+ .where(
+ eq(
+ mediaProcessingBudgets.id,
+ recordingBudgetKey(videoId, job.generation),
+ ),
+ );
+ await tx
+ .update(videoUploads)
+ .set({
+ phase: "processing",
+ processingProgress: 0,
+ processingMessage: "Retrying video processing...",
+ processingError: null,
+ updatedAt: now,
+ })
+ .where(eq(videoUploads.videoId, videoId));
+ return {
+ status: "queued" as const,
+ ownerId: job.ownerId,
+ previousErrorCode: job.errorCode,
+ };
+ });
+}
+
export async function retireDesktopRecordingJobForOutputReplacement(
tx: Parameters<Parameters<ReturnType<typeof db>["transaction"]>[0]>[0],
{
diff --git a/apps/web/lib/media-processing-budget.ts b/apps/web/lib/media-processing-budget.ts
index 0ee62338a..4bbe732d1 100644
--- a/apps/web/lib/media-processing-budget.ts
+++ b/apps/web/lib/media-processing-budget.ts
@@ -13,6 +13,9 @@ export class MediaProcessingBudgetError extends Error {
const key = (parts: string[]) =>
createHash("sha256").update(JSON.stringify(parts)).digest("hex");
+export const recordingBudgetKey = (videoId: string, generation: string) =>
+ key(["recording", videoId, generation]);
+
export function getMediaProcessingReservation(sourceBytes: number) {
if (
!Number.isSafeInteger(sourceBytes) ||
@@ -43,7 +46,7 @@ export async function reserveMediaProcessingBudget(input: {
input.generation,
input.attemptId,
]);
- const recordingKey = key(["recording", input.videoId, input.generation]);
+ const recordingKey = recordingBudgetKey(input.videoId, input.generation);
const expiresAt = new Date(now.getTime() + 30 * 24 * 60 * 60_000);
return db().transaction(async (tx) => {
const limits = [