@@ -15,6 +15,7 @@ import {
15
15
coerceError ,
16
16
RETRYABLE_ERROR_CODES ,
17
17
RetryOptions ,
18
+ safeEmit ,
18
19
sleep ,
19
20
} from "./lib" ;
20
21
import { batchGetJobs } from "./sql/getJobs" ;
@@ -231,7 +232,7 @@ export class LocalQueue {
231
232
private refetchDelayAbortThreshold : number = Infinity ;
232
233
233
234
constructor (
234
- private readonly compiledSharedOptions : CompiledSharedOptions < WorkerPoolOptions > ,
235
+ private readonly ctx : CompiledSharedOptions < WorkerPoolOptions > ,
235
236
private readonly tasks : TaskList ,
236
237
private readonly withPgClient : EnhancedWithPgClient ,
237
238
public readonly workerPool : WorkerPool ,
@@ -244,13 +245,10 @@ export class LocalQueue {
244
245
private readonly continuous : boolean ,
245
246
private readonly onMajorError : ( e : unknown ) => void ,
246
247
) {
247
- this . ttl =
248
- compiledSharedOptions . resolvedPreset . worker . localQueue ?. ttl ?? 5 * MINUTE ;
249
- this . pollInterval =
250
- compiledSharedOptions . resolvedPreset . worker . pollInterval ?? 2 * SECOND ;
248
+ this . ttl = ctx . resolvedPreset . worker . localQueue ?. ttl ?? 5 * MINUTE ;
249
+ this . pollInterval = ctx . resolvedPreset . worker . pollInterval ?? 2 * SECOND ;
251
250
const localQueueRefetchDelayDuration =
252
- compiledSharedOptions . resolvedPreset . worker . localQueue ?. refetchDelay
253
- ?. durationMs ;
251
+ ctx . resolvedPreset . worker . localQueue ?. refetchDelay ?. durationMs ;
254
252
if (
255
253
localQueueRefetchDelayDuration != null &&
256
254
localQueueRefetchDelayDuration > this . pollInterval
@@ -259,8 +257,8 @@ export class LocalQueue {
259
257
`Invalid configuration; 'preset.worker.localQueue.refetchDelay.durationMs' (${ localQueueRefetchDelayDuration } ) must not be larger than 'preset.worker.pollInterval' (${ this . pollInterval } )` ,
260
258
) ;
261
259
}
262
- compiledSharedOptions . events . emit ( "localQueue:init" , {
263
- ctx : compiledSharedOptions ,
260
+ safeEmit ( ctx , "localQueue:init" , {
261
+ ctx : ctx ,
264
262
localQueue : this ,
265
263
} ) ;
266
264
// Immediately enter polling mode.
@@ -276,8 +274,8 @@ export class LocalQueue {
276
274
const oldMode = this . mode ;
277
275
// Override the 'readonly'
278
276
( this . mode as LocalQueueMode ) = newMode ;
279
- this . compiledSharedOptions . events . emit ( "localQueue:setMode" , {
280
- ctx : this . compiledSharedOptions ,
277
+ safeEmit ( this . ctx , "localQueue:setMode" , {
278
+ ctx : this . ctx ,
281
279
localQueue : this ,
282
280
oldMode,
283
281
newMode,
@@ -321,7 +319,7 @@ export class LocalQueue {
321
319
} else {
322
320
// If we're not shutting down, view this as a temporary error (but give
323
321
// Benjie a wrist slap anyway).
324
- this . compiledSharedOptions . logger . error (
322
+ this . ctx . logger . error (
325
323
`GraphileWorkerInternalError<cd483429-3372-42f0-bcf6-c78e045c760d>: Backgrounding should never yield errors when the queue is not RELEASED` ,
326
324
{ error : e } ,
327
325
) ;
@@ -444,8 +442,8 @@ export class LocalQueue {
444
442
}
445
443
const jobsToReturn = this . jobQueue . splice ( 0 , l ) ;
446
444
447
- this . compiledSharedOptions . events . emit ( "localQueue:returnJobs" , {
448
- ctx : this . compiledSharedOptions ,
445
+ safeEmit ( this . ctx , "localQueue:returnJobs" , {
446
+ ctx : this . ctx ,
449
447
localQueue : this ,
450
448
jobs : jobsToReturn ,
451
449
} ) ;
@@ -459,7 +457,7 @@ export class LocalQueue {
459
457
initialError = lastError ;
460
458
}
461
459
462
- this . compiledSharedOptions . logger . error (
460
+ this . ctx . logger . error (
463
461
`Failed to return jobs from local queue to database queue (attempt ${ attempts } /${ maxAttempts } )` ,
464
462
{
465
463
error : e ,
@@ -502,7 +500,7 @@ export class LocalQueue {
502
500
++ attempts ;
503
501
return sleep ( delay ) . then ( ( ) =>
504
502
returnJobs (
505
- this . compiledSharedOptions ,
503
+ this . ctx ,
506
504
this . withPgClient , // We'll handle the retries via onError
507
505
this . workerPool . id ,
508
506
jobsToReturn ,
@@ -517,7 +515,7 @@ export class LocalQueue {
517
515
// `onError` above, since `onError` returns the next promise each time.
518
516
this . background (
519
517
returnJobs (
520
- this . compiledSharedOptions ,
518
+ this . ctx ,
521
519
this . withPgClient , // We'll handle the retries via onError
522
520
this . workerPool . id ,
523
521
jobsToReturn ,
@@ -554,7 +552,7 @@ export class LocalQueue {
554
552
this . background (
555
553
this . _fetch ( ) . catch ( ( e ) => {
556
554
// This should not happen
557
- this . compiledSharedOptions . logger . error ( `Error occurred during fetch` , {
555
+ this . ctx . logger . error ( `Error occurred during fetch` , {
558
556
error : e ,
559
557
} ) ;
560
558
} ) ,
@@ -578,7 +576,7 @@ export class LocalQueue {
578
576
/** How many jobs did we fetch? (Initialize to zero in case of error.) */
579
577
let jobCount = 0 ;
580
578
const refetchDelayOptions =
581
- this . compiledSharedOptions . resolvedPreset . worker . localQueue ?. refetchDelay ;
579
+ this . ctx . resolvedPreset . worker . localQueue ?. refetchDelay ;
582
580
try {
583
581
assert . equal ( this . mode , POLLING , "Can only fetch when in polling mode" ) ;
584
582
assert . equal (
@@ -605,16 +603,16 @@ export class LocalQueue {
605
603
606
604
// The ONLY await in this function.
607
605
const jobs = await batchGetJobs (
608
- this . compiledSharedOptions ,
606
+ this . ctx ,
609
607
this . withPgClient ,
610
608
this . tasks ,
611
609
this . workerPool . id ,
612
610
null , // `flagsToSkip` is not set, see `LocalQueue.getJob`
613
611
this . getJobBatchSize ,
614
612
) ;
615
613
616
- this . compiledSharedOptions . events . emit ( "localQueue:getJobs:complete" , {
617
- ctx : this . compiledSharedOptions ,
614
+ safeEmit ( this . ctx , "localQueue:getJobs:complete" , {
615
+ ctx : this . ctx ,
618
616
localQueue : this ,
619
617
jobs,
620
618
} ) ;
@@ -634,7 +632,7 @@ export class LocalQueue {
634
632
this . receivedJobs ( jobs ) ;
635
633
} catch ( e ) {
636
634
// Error happened; rely on poll interval.
637
- this . compiledSharedOptions . logger . error (
635
+ this . ctx . logger . error (
638
636
`Error occurred fetching jobs; will try again on next poll interval. Error: ${ e } ` ,
639
637
{ error : e } ,
640
638
) ;
@@ -674,8 +672,8 @@ export class LocalQueue {
674
672
this . refetchDelayCompleteOrAbort ,
675
673
refetchDelayMs ,
676
674
) ;
677
- this . compiledSharedOptions . events . emit ( "localQueue:refetchDelay:start" , {
678
- ctx : this . compiledSharedOptions ,
675
+ safeEmit ( this . ctx , "localQueue:refetchDelay:start" , {
676
+ ctx : this . ctx ,
679
677
localQueue : this ,
680
678
jobCount,
681
679
threshold : refetchDelayOptions ?. threshold ?? 0 ,
@@ -720,20 +718,17 @@ export class LocalQueue {
720
718
// Force refetch because we've been notified of so many jobs!
721
719
this . refetchDelayFetchOnComplete = true ;
722
720
723
- this . compiledSharedOptions . events . emit ( "localQueue:refetchDelay:abort" , {
724
- ctx : this . compiledSharedOptions ,
721
+ safeEmit ( this . ctx , "localQueue:refetchDelay:abort" , {
722
+ ctx : this . ctx ,
725
723
localQueue : this ,
726
724
count : this . refetchDelayCounter ,
727
725
abortThreshold : this . refetchDelayAbortThreshold ,
728
726
} ) ;
729
727
} else {
730
- this . compiledSharedOptions . events . emit (
731
- "localQueue:refetchDelay:expired" ,
732
- {
733
- ctx : this . compiledSharedOptions ,
734
- localQueue : this ,
735
- } ,
736
- ) ;
728
+ safeEmit ( this . ctx , "localQueue:refetchDelay:expired" , {
729
+ ctx : this . ctx ,
730
+ localQueue : this ,
731
+ } ) ;
737
732
}
738
733
739
734
if ( this . mode === POLLING && this . refetchDelayFetchOnComplete ) {
@@ -789,7 +784,7 @@ export class LocalQueue {
789
784
if ( flagsToSkip !== null ) {
790
785
// PERF: we could actually batch for similar flags, I guess.
791
786
const jobsPromise = batchGetJobs (
792
- this . compiledSharedOptions ,
787
+ this . ctx ,
793
788
this . withPgClient ,
794
789
this . tasks ,
795
790
this . workerPool . id ,
0 commit comments