-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(supervisor): cancel pending delayed snapshots when the run completes or disconnects #3894
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f5b8db
fix: cancel pending delayed snapshots when the run completes or disco…
myftija e7a6e76
fix: cancel pending delayed snapshots regardless of completion outcome
myftija 3b4c9db
chore: add server-changes entry
myftija 60d270f
fix: cancel pending delayed snapshot before the async completion call
myftija 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
130 changes: 130 additions & 0 deletions
130
apps/supervisor/src/services/computeSnapshotService.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,130 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { setTimeout as sleep } from "node:timers/promises"; | ||
| import { ComputeSnapshotService } from "./computeSnapshotService.js"; | ||
| import type { ComputeWorkloadManager } from "../workloadManager/compute.js"; | ||
| import type { SupervisorHttpClient } from "@trigger.dev/core/v3/workers"; | ||
|
|
||
| // The TimerWheel ticks every 100ms, so a 200ms delay dispatches within ~300ms. | ||
| const DELAY_MS = 200; | ||
| // Long enough that a pending snapshot would certainly have dispatched. | ||
| const SETTLE_MS = 600; | ||
|
|
||
| function createService() { | ||
| const snapshot = vi.fn(async (_opts: { runnerId: string; metadata: Record<string, string> }) => true); | ||
|
|
||
| const computeManager = { | ||
| snapshotDelayMs: DELAY_MS, | ||
| snapshotDispatchLimit: 1, | ||
| snapshot, | ||
| } as unknown as ComputeWorkloadManager; | ||
|
|
||
| const service = new ComputeSnapshotService({ | ||
| computeManager, | ||
| workerClient: {} as SupervisorHttpClient, | ||
| wideEventOpts: { service: "supervisor-test", env: {}, enabled: false }, | ||
| }); | ||
|
|
||
| return { service, snapshot }; | ||
| } | ||
|
|
||
| function delayedSnapshot(runnerId = "runner-1") { | ||
| return { | ||
| runnerId, | ||
| runFriendlyId: "run_1", | ||
| snapshotFriendlyId: "snapshot_1", | ||
| }; | ||
| } | ||
|
|
||
| describe("ComputeSnapshotService", () => { | ||
| it("dispatches a scheduled snapshot after the delay", async () => { | ||
| const { service, snapshot } = createService(); | ||
| try { | ||
| service.schedule("run_1", delayedSnapshot()); | ||
|
|
||
| await vi.waitFor(() => expect(snapshot).toHaveBeenCalledTimes(1), { timeout: 2_000 }); | ||
| expect(snapshot).toHaveBeenCalledWith({ | ||
| runnerId: "runner-1", | ||
| metadata: { runId: "run_1", snapshotFriendlyId: "snapshot_1" }, | ||
| }); | ||
| } finally { | ||
| service.stop(); | ||
| } | ||
| }); | ||
|
|
||
| it("cancel before the delay expires prevents the dispatch", async () => { | ||
| const { service, snapshot } = createService(); | ||
| try { | ||
| service.schedule("run_1", delayedSnapshot()); | ||
|
|
||
| expect(service.cancel("run_1")).toBe(true); | ||
|
|
||
| await sleep(SETTLE_MS); | ||
| expect(snapshot).not.toHaveBeenCalled(); | ||
| } finally { | ||
| service.stop(); | ||
| } | ||
| }); | ||
|
|
||
| it("cancel returns false when nothing is pending", () => { | ||
| const { service } = createService(); | ||
| try { | ||
| expect(service.cancel("run_1")).toBe(false); | ||
| } finally { | ||
| service.stop(); | ||
| } | ||
| }); | ||
|
|
||
| it("cancel with a matching runnerId cancels the pending snapshot", async () => { | ||
| const { service, snapshot } = createService(); | ||
| try { | ||
| service.schedule("run_1", delayedSnapshot("runner-a")); | ||
|
|
||
| expect(service.cancel("run_1", "runner-a")).toBe(true); | ||
|
|
||
| await sleep(SETTLE_MS); | ||
| expect(snapshot).not.toHaveBeenCalled(); | ||
| } finally { | ||
| service.stop(); | ||
| } | ||
| }); | ||
|
|
||
| it("cancel with a different runnerId leaves the pending snapshot alone", async () => { | ||
| const { service, snapshot } = createService(); | ||
| try { | ||
| service.schedule("run_1", delayedSnapshot("runner-a")); | ||
|
|
||
| // A stale runner for a reassigned run must not cancel the new runner's snapshot. | ||
| expect(service.cancel("run_1", "runner-b")).toBe(false); | ||
|
|
||
| await vi.waitFor(() => expect(snapshot).toHaveBeenCalledTimes(1), { timeout: 2_000 }); | ||
| expect(snapshot).toHaveBeenCalledWith( | ||
| expect.objectContaining({ runnerId: "runner-a" }) | ||
| ); | ||
| } finally { | ||
| service.stop(); | ||
| } | ||
| }); | ||
|
|
||
| it("re-scheduling the same run replaces the pending snapshot", async () => { | ||
| const { service, snapshot } = createService(); | ||
| try { | ||
| service.schedule("run_1", delayedSnapshot()); | ||
| service.schedule("run_1", { | ||
| runnerId: "runner-1", | ||
| runFriendlyId: "run_1", | ||
| snapshotFriendlyId: "snapshot_2", | ||
| }); | ||
|
|
||
| await vi.waitFor(() => expect(snapshot).toHaveBeenCalledTimes(1), { timeout: 2_000 }); | ||
| await sleep(SETTLE_MS); | ||
|
|
||
| expect(snapshot).toHaveBeenCalledTimes(1); | ||
| expect(snapshot).toHaveBeenCalledWith({ | ||
| runnerId: "runner-1", | ||
| metadata: { runId: "run_1", snapshotFriendlyId: "snapshot_2" }, | ||
| }); | ||
| } finally { | ||
| service.stop(); | ||
| } | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.