@@ -87,6 +87,8 @@ export class JobProcessor {
87
87
88
88
private isLockingOnTheFly = false ;
89
89
90
+ private isJobQueueFilling = new Map < string , boolean > ( ) ;
91
+
90
92
private isRunning = true ;
91
93
92
94
private processInterval ?: ReturnType < typeof setInterval > ;
@@ -211,19 +213,24 @@ export class JobProcessor {
211
213
return ;
212
214
}
213
215
216
+ this . isLockingOnTheFly = true ;
217
+
214
218
// Set that we are running this
215
219
try {
216
- this . isLockingOnTheFly = true ;
217
-
218
220
// Grab a job that needs to be locked
219
221
const job = this . jobsToLock . pop ( ) ;
220
222
221
223
if ( job ) {
224
+ if ( this . isJobQueueFilling . has ( job . attrs . name ) ) {
225
+ log . extend ( 'lockOnTheFly' ) ( 'jobQueueFilling already running for: %s' , job . attrs . name ) ;
226
+ return ;
227
+ }
228
+
222
229
// If locking limits have been hit, stop locking on the fly.
223
230
// Jobs that were waiting to be locked will be picked up during a
224
231
// future locking interval.
225
232
if ( ! this . shouldLock ( job . attrs . name ) ) {
226
- log . extend ( 'lockOnTheFly' ) ( 'lock limit hit for: [%s]' , job . attrs . name ) ;
233
+ log . extend ( 'lockOnTheFly' ) ( 'lock limit hit for: [%s:%S ]' , job . attrs . name , job . attrs . _id ) ;
227
234
this . jobsToLock = [ ] ;
228
235
return ;
229
236
}
@@ -253,8 +260,9 @@ export class JobProcessor {
253
260
}
254
261
255
262
log . extend ( 'lockOnTheFly' ) (
256
- 'found job [%s] that can be locked on the fly' ,
257
- jobToEnqueue . attrs . name
263
+ 'found job [%s:%s] that can be locked on the fly' ,
264
+ jobToEnqueue . attrs . name ,
265
+ jobToEnqueue . attrs . _id
258
266
) ;
259
267
this . updateStatus ( jobToEnqueue . attrs . name , 'locked' , + 1 ) ;
260
268
this . lockedJobs . push ( jobToEnqueue ) ;
@@ -302,18 +310,20 @@ export class JobProcessor {
302
310
* @returns {undefined }
303
311
*/
304
312
private async jobQueueFilling ( name : string ) : Promise < void > {
305
- // Don't lock because of a limit we have set (lockLimit, etc)
306
- if ( ! this . shouldLock ( name ) ) {
307
- log . extend ( 'jobQueueFilling' ) ( 'lock limit reached in queue filling for [%s]' , name ) ;
308
- return ;
309
- }
313
+ this . isJobQueueFilling . set ( name , true ) ;
310
314
311
- // Set the date of the next time we are going to run _processEvery function
312
- const now = new Date ( ) ;
313
- this . nextScanAt = new Date ( now . valueOf ( ) + this . processEvery ) ;
314
-
315
- // For this job name, find the next job to run and lock it!
316
315
try {
316
+ // Don't lock because of a limit we have set (lockLimit, etc)
317
+ if ( ! this . shouldLock ( name ) ) {
318
+ log . extend ( 'jobQueueFilling' ) ( 'lock limit reached in queue filling for [%s]' , name ) ;
319
+ return ;
320
+ }
321
+
322
+ // Set the date of the next time we are going to run _processEvery function
323
+ const now = new Date ( ) ;
324
+ this . nextScanAt = new Date ( now . valueOf ( ) + this . processEvery ) ;
325
+
326
+ // For this job name, find the next job to run and lock it!
317
327
const job = await this . findAndLockNextJob ( name , this . agenda . definitions [ name ] ) ;
318
328
319
329
// Still have the job?
@@ -354,6 +364,8 @@ export class JobProcessor {
354
364
} catch ( error ) {
355
365
log . extend ( 'jobQueueFilling' ) ( '[%s] job lock failed while filling queue' , name , error ) ;
356
366
this . agenda . emit ( 'error' , error ) ;
367
+ } finally {
368
+ this . isJobQueueFilling . delete ( name ) ;
357
369
}
358
370
}
359
371
0 commit comments