Skip to content

Commit 13cb098

Browse files
fix(cron): remove deleted job sessions (#119520)
* fix(cron): remove deleted job sessions * fix(cron): guard deferred session cleanup * test(cron): remove invalid cleanup await * fix(cron): fence deferred session cleanup * fix(cron): share removal cleanup fence Co-authored-by: Goutam Adwant <[email protected]> * fix(cron): preserve stored add result type Co-authored-by: Goutam Adwant <[email protected]> * fix(cron): release lock before session cleanup Co-authored-by: Goutam Adwant <[email protected]> * fix(cron): avoid materializing cleanup store Co-authored-by: Goutam Adwant <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 389c102 commit 13cb098

7 files changed

Lines changed: 603 additions & 11 deletions

File tree

‎src/cron/active-jobs.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Unit coverage for the active-job accounting the heartbeat busy guard depends on.
2-
import { afterEach, describe, expect, it } from "vitest";
2+
import { afterEach, describe, expect, it, vi } from "vitest";
33
import {
44
clearCronJobActive,
55
hasActiveCronJobs,
@@ -8,6 +8,7 @@ import {
88
noteActiveCronJobRemoval,
99
noteActiveCronJobScheduleMutation,
1010
noteActiveCronJobTriggerMutation,
11+
onCronJobInactive,
1112
resetCronActiveJobs,
1213
} from "./active-jobs.js";
1314

@@ -50,6 +51,19 @@ describe("hasActiveCronJobsExceptMarker", () => {
5051
});
5152

5253
describe("active cron schedule ownership", () => {
54+
it("notifies only the removed marker when a same-id run replaces it", () => {
55+
const removedMarker = markCronJobActive("reused-job");
56+
const onRemovedInactive = vi.fn();
57+
onCronJobInactive(noteActiveCronJobRemoval("reused-job"), onRemovedInactive);
58+
const replacementMarker = markCronJobActive("reused-job");
59+
60+
clearCronJobActive("reused-job", replacementMarker);
61+
expect(onRemovedInactive).not.toHaveBeenCalled();
62+
63+
clearCronJobActive("reused-job", removedMarker);
64+
expect(onRemovedInactive).toHaveBeenCalledOnce();
65+
});
66+
5367
it("records durable job removal without releasing the active run marker", () => {
5468
const marker = markCronJobActive("removed-job");
5569

‎src/cron/active-jobs.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export type CronActiveJobMarker = {
1818
triggerMutated?: true;
1919
jobRemoved?: true;
2020
preserveAcrossGenerationAdvance?: boolean;
21+
onInactive?: Set<() => void>;
22+
inactiveNotified?: true;
2123
};
2224

2325
function getCronActiveJobState(): CronActiveJobState {
@@ -60,6 +62,17 @@ function notifyActiveCronJobWaitersIfEmpty(state: CronActiveJobState) {
6062
state.emptyWaiters.clear();
6163
}
6264

65+
function notifyCronJobInactive(marker: CronActiveJobMarker) {
66+
if (marker.inactiveNotified) {
67+
return;
68+
}
69+
marker.inactiveNotified = true;
70+
for (const callback of marker.onInactive ?? []) {
71+
callback();
72+
}
73+
marker.onInactive?.clear();
74+
}
75+
6376
/** Marks a cron job id as currently executing for duplicate-run suppression. */
6477
export function markCronJobActive(
6578
jobId: string,
@@ -93,6 +106,11 @@ export function clearCronJobActive(jobId: string, marker?: CronActiveJobMarker)
93106
(!marker || (marker.jobId === jobId && marker.token === activeMarker.token))
94107
) {
95108
state.activeJobs.delete(jobId);
109+
notifyCronJobInactive(activeMarker);
110+
} else if (marker?.jobId === jobId) {
111+
// The caller is finalizing this exact run even when a same-id replacement
112+
// now owns the map slot. Notify only the retired marker's listeners.
113+
notifyCronJobInactive(marker);
96114
}
97115
notifyActiveCronJobWaitersIfEmpty(state);
98116
}
@@ -126,9 +144,9 @@ export function noteActiveCronJobTriggerMutation(jobId: string): void {
126144
}
127145

128146
/** Retires the admitted job identity after its deletion becomes durable. */
129-
export function noteActiveCronJobRemoval(jobId: string): void {
147+
export function noteActiveCronJobRemoval(jobId: string): CronActiveJobMarker | undefined {
130148
if (!jobId) {
131-
return;
149+
return undefined;
132150
}
133151
const state = getCronActiveJobState();
134152
const marker = state.activeJobs.get(jobId);
@@ -137,7 +155,9 @@ export function noteActiveCronJobRemoval(jobId: string): void {
137155
// Keep its marker until completion so duplicate-run guards remain intact.
138156
marker.scheduleMutated = true;
139157
marker.jobRemoved = true;
158+
return marker;
140159
}
160+
return undefined;
141161
}
142162

143163
/** Returns whether the given cron job id is currently executing in this process. */
@@ -150,6 +170,19 @@ export function isCronJobActive(jobId: string) {
150170
return marker ? isMarkerActiveInGeneration(marker, state.generation) : false;
151171
}
152172

173+
/** Runs a callback when the exact cron job no longer has an active in-process run. */
174+
export function onCronJobInactive(
175+
marker: CronActiveJobMarker | undefined,
176+
callback: () => void,
177+
): void {
178+
if (!marker || marker.inactiveNotified) {
179+
callback();
180+
return;
181+
}
182+
marker.onInactive ??= new Set<() => void>();
183+
marker.onInactive.add(callback);
184+
}
185+
153186
export function isCronActiveJobMarkerCurrent(marker: CronActiveJobMarker | undefined) {
154187
if (!marker) {
155188
return true;
@@ -226,6 +259,7 @@ export function advanceCronActiveJobGeneration() {
226259
}
227260
if (marker.generation < state.generation - 1) {
228261
state.activeJobs.delete(jobId);
262+
notifyCronJobInactive(marker);
229263
}
230264
}
231265
notifyActiveCronJobWaitersIfEmpty(state);
@@ -235,6 +269,9 @@ export function advanceCronActiveJobGeneration() {
235269
export function resetCronActiveJobs() {
236270
const state = getCronActiveJobState();
237271
state.generation += 1;
272+
for (const marker of state.activeJobs.values()) {
273+
notifyCronJobInactive(marker);
274+
}
238275
state.activeJobs.clear();
239276
notifyActiveCronJobWaitersIfEmpty(state);
240277
}

0 commit comments

Comments
 (0)