@@ -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
2325function 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. */
6477export 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+
153186export 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() {
235269export 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