Repository navigation
fix: faster, cheaper Loom imports: fewer progress webhooks and real restart checks #3118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
apps/media-server/src/__tests__/loom-importer/progress-webhooks.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { ProgressWebhookThrottle } from "../../loom-importer/job"; | ||
|
|
||
| describe("ProgressWebhookThrottle", () => { | ||
| test("sends the first update for a job", () => { | ||
| const throttle = new ProgressWebhookThrottle(15_000); | ||
| expect(throttle.shouldSend("job-1", "downloading", 0)).toBe(true); | ||
| }); | ||
|
|
||
| test("holds same-phase updates inside the interval", () => { | ||
| const throttle = new ProgressWebhookThrottle(15_000); | ||
| throttle.shouldSend("job-1", "downloading", 0); | ||
| expect(throttle.shouldSend("job-1", "downloading", 1_000)).toBe(false); | ||
| expect(throttle.shouldSend("job-1", "downloading", 14_999)).toBe(false); | ||
| }); | ||
|
|
||
| test("sends again once the interval passes", () => { | ||
| const throttle = new ProgressWebhookThrottle(15_000); | ||
| throttle.shouldSend("job-1", "downloading", 0); | ||
| expect(throttle.shouldSend("job-1", "downloading", 15_000)).toBe(true); | ||
| expect(throttle.shouldSend("job-1", "downloading", 20_000)).toBe(false); | ||
| expect(throttle.shouldSend("job-1", "downloading", 30_000)).toBe(true); | ||
| }); | ||
|
|
||
| test("sends a phase change straight away", () => { | ||
| const throttle = new ProgressWebhookThrottle(15_000); | ||
| throttle.shouldSend("job-1", "downloading", 0); | ||
| expect(throttle.shouldSend("job-1", "uploading", 1)).toBe(true); | ||
| expect(throttle.shouldSend("job-1", "uploading", 2)).toBe(false); | ||
| }); | ||
|
|
||
| test("tracks jobs separately", () => { | ||
| const throttle = new ProgressWebhookThrottle(15_000); | ||
| throttle.shouldSend("job-1", "downloading", 0); | ||
| expect(throttle.shouldSend("job-2", "downloading", 1)).toBe(true); | ||
| }); | ||
|
|
||
| test("forgets finished jobs", () => { | ||
| const throttle = new ProgressWebhookThrottle(15_000); | ||
| throttle.shouldSend("job-1", "downloading", 0); | ||
| throttle.forget("job-1"); | ||
| expect(throttle.shouldSend("job-1", "downloading", 1)).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("@cap/env", () => ({ | ||
| serverEnv: () => ({ | ||
| MEDIA_SERVER_URL: "https://worker.example.com", | ||
| LOOM_IMPORTER_URL: "https://media.example.com/loom-importer", | ||
| MEDIA_SERVER_WEBHOOK_SECRET: "fixture-secret", | ||
| }), | ||
| })); | ||
| vi.mock("@cap/database", () => ({ db: vi.fn() })); | ||
|
|
||
| import { getLoomImporterJobState } from "@/lib/loom-media-dispatch"; | ||
|
|
||
| describe("getLoomImporterJobState", () => { | ||
| afterEach(() => vi.unstubAllGlobals()); | ||
|
|
||
| it.each([ | ||
| [{ jobId: "job", videoId: "video", phase: "downloading" }, "running"], | ||
| [{ jobId: "job", videoId: "video", phase: "queued" }, "running"], | ||
| [{ jobId: "job", videoId: "video", phase: "complete" }, "finished"], | ||
| [{ jobId: "job", videoId: "video", phase: "error" }, "finished"], | ||
| [{ jobId: "job", videoId: "video", phase: "cancelled" }, "finished"], | ||
| [{ jobId: "job", videoId: "other", phase: "error" }, "unknown"], | ||
| ])("reads the importer's job %j", async (body, expected) => { | ||
| const fetch = vi.fn().mockResolvedValue(Response.json(body)); | ||
| vi.stubGlobal("fetch", fetch); | ||
| await expect( | ||
| getLoomImporterJobState({ jobId: "job", videoId: "video" }), | ||
| ).resolves.toBe(expected); | ||
| expect(fetch).toHaveBeenCalledWith( | ||
| "https://media.example.com/loom-importer/video/process/job/status", | ||
| expect.objectContaining({ redirect: "error" }), | ||
| ); | ||
| }); | ||
|
|
||
| it("knows a job the importer no longer has", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn().mockResolvedValue(new Response("missing", { status: 404 })), | ||
| ); | ||
| await expect( | ||
| getLoomImporterJobState({ jobId: "job", videoId: "video" }), | ||
| ).resolves.toBe("missing"); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [() => Promise.resolve(new Response("down", { status: 503 }))], | ||
| [() => Promise.reject(new Error("network"))], | ||
| ])("cannot tell when the importer does not answer: %#", async (respond) => { | ||
| vi.stubGlobal("fetch", vi.fn().mockImplementation(respond)); | ||
| await expect( | ||
| getLoomImporterJobState({ jobId: "job", videoId: "video" }), | ||
| ).resolves.toBe("unknown"); | ||
| }); | ||
|
|
||
| it("looks a video up in the importer's job list when there is no job id", async () => { | ||
| const fetch = vi.fn().mockResolvedValue( | ||
| Response.json({ | ||
| jobs: [{ jobId: "other", videoId: "other-video", phase: "queued" }], | ||
| }), | ||
| ); | ||
| vi.stubGlobal("fetch", fetch); | ||
| await expect(getLoomImporterJobState({ videoId: "video" })).resolves.toBe( | ||
| "missing", | ||
| ); | ||
| fetch.mockResolvedValue( | ||
| Response.json({ | ||
| jobs: [{ jobId: "j", videoId: "video", phase: "queued" }], | ||
| }), | ||
| ); | ||
| await expect(getLoomImporterJobState({ videoId: "video" })).resolves.toBe( | ||
| "running", | ||
| ); | ||
| expect(fetch).toHaveBeenCalledWith( | ||
| "https://media.example.com/loom-importer/video/status", | ||
| expect.anything(), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One-hour fallback can restart a still-running import
Medium Severity
If a workflow fails after dispatch and worker checks are inconclusive, an error row just over one hour old now permits another import. That does not establish that the original worker has stopped: importer jobs explicitly receive a four-hour lifetime, and media jobs also extend their deadlines up to four hours. Lost dispatch acknowledgements or unavailable progress/status endpoints can therefore leave a live worker behind this branch. Redrive does not cancel it before replacing the run and multipart-upload receipts, so the original attempt loses upload ownership while duplicate work starts. Retain a fallback covering the actual worker lifetime, or require confirmed termination/cancellation before restarting an unconfirmed dispatch.