Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: cancel Loom import runs stuck pending and restart abandoned unconfirmed imports
Richie McIlroy2033ddf6a2d7
diff --git a/apps/web/__tests__/unit/loom-import-start.test.ts b/apps/web/__tests__/unit/loom-import-start.test.tsindex 6c13975a3..4af353608 100644--- a/apps/web/__tests__/unit/loom-import-start.test.ts+++ b/apps/web/__tests__/unit/loom-import-start.test.ts@@ -297,6 +297,45 @@ describe("Loom workflow startup receipts", () => {},);+ it("cancels a run that never left pending and treats it as inactive", async () => {+ const run = {+ status: Promise.resolve("pending"),+ createdAt: Promise.resolve(new Date(Date.now() - 2 * 60 * 60 * 1000)),+ cancel: vi.fn(async () => {+ run.status = Promise.resolve("cancelled");+ }),+ };+ mocks.getRun.mockReturnValue(run);+ expect(+ await getLoomImportRunState({ runId: "run-1", dispatch: "uncertain" }),+ ).toBe("inactive");+ expect(run.cancel).toHaveBeenCalledTimes(1);+ });++ it("leaves a recently queued run pending", async () => {+ const cancel = vi.fn();+ mocks.getRun.mockReturnValue({+ status: Promise.resolve("pending"),+ createdAt: Promise.resolve(new Date(Date.now() - 10 * 60 * 1000)),+ cancel,+ });+ expect(+ await getLoomImportRunState({ runId: "run-1", dispatch: "uncertain" }),+ ).toBe("pending");+ expect(cancel).not.toHaveBeenCalled();+ });++ it("keeps an old pending run pending when it cannot be cancelled", async () => {+ mocks.getRun.mockReturnValue({+ status: Promise.resolve("pending"),+ createdAt: Promise.resolve(new Date(Date.now() - 2 * 60 * 60 * 1000)),+ cancel: vi.fn().mockRejectedValue(new Error("Unavailable")),+ });+ expect(+ await getLoomImportRunState({ runId: "run-1", dispatch: "uncertain" }),+ ).toBe("pending");+ });+it("holds startup while the run cannot be queried", async () => {mocks.getRun.mockReturnValue({status: Promise.reject(new Error("Not found yet")),diff --git a/apps/web/__tests__/unit/loom-importer-redrive.test.ts b/apps/web/__tests__/unit/loom-importer-redrive.test.tsindex 2044d3185..df93902dc 100644--- a/apps/web/__tests__/unit/loom-importer-redrive.test.ts+++ b/apps/web/__tests__/unit/loom-importer-redrive.test.ts@@ -225,6 +225,23 @@ describe("redriveLoomImport", () => {});});+ it("restarts an unconfirmed import left processing once its worker can no longer be running", async () => {+ mocks.runState.mockResolvedValue("unconfirmed");+ mocks.rows = [+ failedImport({ phase: "processing", uploadUpdatedAt: hoursAgo(0.5) }),+ ];+ await expect(redriveLoomImport("video", now)).resolves.toMatchObject({+ status: "skipped",+ reason: "phase processing",+ });+ mocks.rows = [+ failedImport({ phase: "processing", uploadUpdatedAt: hoursAgo(2) }),+ ];+ await expect(redriveLoomImport("video", now)).resolves.toMatchObject({+ status: "restarted",+ });+ });+it("does not start a workflow when another restart claimed the row", async () => {mocks.affectedRows = 0;await expect(redriveLoomImport("video", now)).resolves.toMatchObject({diff --git a/apps/web/lib/loom-import-redrive.ts b/apps/web/lib/loom-import-redrive.tsindex 1653c2029..00ceb5e58 100644--- a/apps/web/lib/loom-import-redrive.ts+++ b/apps/web/lib/loom-import-redrive.ts@@ -62,8 +62,8 @@ export async function redriveLoomImport(const runState = await getLoomImportRunState(row.metadata?.loomImportRun);const abandoned =row.phase === "processing" &&- runState === "inactive" &&- age > ABANDONED_PROCESSING_AGE_MS;+ ((runState === "inactive" && age > ABANDONED_PROCESSING_AGE_MS) ||+ (runState === "unconfirmed" && age >= FINISHED_WORKER_AGE_MS));if (row.phase !== "error" && !stalled && !abandoned)return { videoId, status: "skipped", reason: `phase ${row.phase}` };if (runState === "pending" || runState === "active")diff --git a/apps/web/lib/loom-import-start.ts b/apps/web/lib/loom-import-start.tsindex dd2f819b1..8ba13a934 100644--- a/apps/web/lib/loom-import-start.ts+++ b/apps/web/lib/loom-import-start.ts@@ -57,13 +57,29 @@ export class LoomImportStartError extends Error {export const LOOM_IMPORT_CONFIRMATION_MESSAGE ="We're confirming whether your Loom import started. Please contact support before retrying.";+const STUCK_PENDING_RUN_MS = 60 * 60 * 1000;++async function cancelStuckPendingRun(run: ReturnType<typeof getRun>) {+ try {+ const createdAt = await run.createdAt;+ if (Date.now() - createdAt.getTime() < STUCK_PENDING_RUN_MS) return false;+ await run.cancel();+ return (await run.status) === "cancelled";+ } catch {+ return false;+ }+}+export async function getLoomImportRunState(receipt?: LoomImportRun,): Promise<"inactive" | "pending" | "active" | "unconfirmed"> {if (!receipt || receipt.dispatch === "rejected") return "inactive";+ const run = getRun(receipt.runId);let status: string;try {- status = await getRun(receipt.runId).status;+ status = await run.status;+ if (status === "pending" && (await cancelStuckPendingRun(run)))+ status = "cancelled";} catch (error) {return WorkflowRunNotFoundError.is(error) &&receipt.dispatch === "uncertain" &&