@@ -47,13 +47,21 @@ export class JobProcessor {
47
47
version,
48
48
queueName : this . agenda . attrs . name ,
49
49
totalQueueSizeDB : await this . agenda . db . getQueueSize ( ) ,
50
+ internal : {
51
+ localQueueProcessing : this . localQueueProcessing
52
+ } ,
50
53
config : {
51
54
totalLockLimit : this . totalLockLimit ,
52
55
maxConcurrency : this . maxConcurrency ,
53
56
processEvery : this . processEvery
54
57
} ,
55
58
jobStatus,
56
- queuedJobs : this . jobQueue . length ,
59
+ queuedJobs : ! fullDetails
60
+ ? this . jobQueue . length
61
+ : this . jobQueue . getQueue ( ) . map ( job => ( {
62
+ ...job . toJson ( ) ,
63
+ canceled : job . canceled ?. message || job . canceled
64
+ } ) ) ,
57
65
runningJobs : ! fullDetails
58
66
? this . runningJobs . length
59
67
: this . runningJobs . map ( job => ( {
@@ -363,7 +371,7 @@ export class JobProcessor {
363
371
* handledJobs keeps list of already processed jobs
364
372
* @returns {undefined }
365
373
*/
366
- private jobProcessing ( handledJobs : IJobParameters [ '_id' ] [ ] = [ ] ) {
374
+ private async jobProcessing ( handledJobs : IJobParameters [ '_id' ] [ ] = [ ] ) {
367
375
// Ensure we have jobs
368
376
if ( this . jobQueue . length === 0 ) {
369
377
return ;
@@ -381,7 +389,14 @@ export class JobProcessor {
381
389
return ;
382
390
}
383
391
384
- log . extend ( 'jobProcessing' ) ( '[%s:%s] there is a job to process' , job . attrs . name , job . attrs . _id ) ;
392
+ log . extend ( 'jobProcessing' ) (
393
+ '[%s:%s] there is a job to process (priority = %d)' ,
394
+ job . attrs . name ,
395
+ job . attrs . _id ,
396
+ job . attrs . priority
397
+ ) ;
398
+
399
+ this . jobQueue . remove ( job ) ;
385
400
386
401
// If the 'nextRunAt' time is older than the current time, run the job
387
402
// Otherwise, setTimeout that gets called at the time of 'nextRunAt'
@@ -394,15 +409,39 @@ export class JobProcessor {
394
409
this . runOrRetry ( job ) ;
395
410
} else {
396
411
const runIn = job . attrs . nextRunAt . getTime ( ) - now . getTime ( ) ;
397
- log . extend ( 'jobProcessing' ) (
398
- '[%s:%s] nextRunAt is in the future, calling setTimeout(%d)' ,
399
- job . attrs . name ,
400
- job . attrs . _id ,
401
- runIn
402
- ) ;
403
- setTimeout ( ( ) => {
404
- this . jobProcessing ( ) ;
405
- } , runIn ) ;
412
+ if ( runIn > this . processEvery ) {
413
+ // this job is not in the near future, remove it (it will be picked up later)
414
+ log . extend ( 'runOrRetry' ) (
415
+ '[%s:%s] job is too far away, freeing it up' ,
416
+ job . attrs . name ,
417
+ job . attrs . _id
418
+ ) ;
419
+ let lockedJobIndex = this . lockedJobs . indexOf ( job ) ;
420
+ if ( lockedJobIndex === - 1 ) {
421
+ // lookup by id
422
+ lockedJobIndex = this . lockedJobs . findIndex (
423
+ j => j . attrs . _id ?. toString ( ) === job . attrs . _id ?. toString ( )
424
+ ) ;
425
+ }
426
+ if ( lockedJobIndex === - 1 ) {
427
+ throw new Error ( `cannot find job ${ job . attrs . _id } in locked jobs queue?` ) ;
428
+ }
429
+
430
+ this . lockedJobs . splice ( lockedJobIndex , 1 ) ;
431
+ this . updateStatus ( job . attrs . name , 'locked' , - 1 ) ;
432
+ } else {
433
+ log . extend ( 'jobProcessing' ) (
434
+ '[%s:%s] nextRunAt is in the future, calling setTimeout(%d)' ,
435
+ job . attrs . name ,
436
+ job . attrs . _id ,
437
+ runIn
438
+ ) ;
439
+ // re add to queue (puts it at the ned of the queue)
440
+ this . jobQueue . push ( job ) ;
441
+ setTimeout ( ( ) => {
442
+ this . jobProcessing ( ) ;
443
+ } , runIn ) ;
444
+ }
406
445
}
407
446
408
447
handledJobs . push ( job . attrs . _id ) ;
@@ -430,39 +469,13 @@ export class JobProcessor {
430
469
return ;
431
470
}
432
471
433
- this . jobQueue . remove ( job ) ;
434
-
435
472
const jobDefinition = this . agenda . definitions [ job . attrs . name ] ;
436
473
const status = this . jobStatus [ job . attrs . name ] ;
437
474
438
475
if (
439
476
( ! jobDefinition . concurrency || ! status || status . running < jobDefinition . concurrency ) &&
440
477
this . runningJobs . length < this . maxConcurrency
441
478
) {
442
- if ( job . isDead ( ) ) {
443
- // not needed to update lockedAt in databsase, as the timeout has been reached,
444
- // and it will be picked up anyways again
445
- log . extend ( 'runOrRetry' ) (
446
- '[%s:%s] job lock has expired, freeing it up' ,
447
- job . attrs . name ,
448
- job . attrs . _id
449
- ) ;
450
- let lockedJobIndex = this . lockedJobs . indexOf ( job ) ;
451
- if ( lockedJobIndex === - 1 ) {
452
- // lookup by id
453
- lockedJobIndex = this . lockedJobs . findIndex (
454
- j => j . attrs . _id ?. toString ( ) === job . attrs . _id ?. toString ( )
455
- ) ;
456
- }
457
- if ( lockedJobIndex === - 1 ) {
458
- throw new Error ( `cannot find job ${ job . attrs . _id } in locked jobs queue?` ) ;
459
- }
460
-
461
- this . lockedJobs . splice ( lockedJobIndex , 1 ) ;
462
- this . updateStatus ( job . attrs . name , 'locked' , - 1 ) ;
463
- return ;
464
- }
465
-
466
479
// Add to local "running" queue
467
480
this . runningJobs . push ( job ) ;
468
481
this . updateStatus ( job . attrs . name , 'running' , 1 ) ;
@@ -474,9 +487,9 @@ export class JobProcessor {
474
487
const checkIfJobIsStillAlive = ( ) => {
475
488
// check every "this.agenda.definitions[job.attrs.name].lockLifetime / 2"" (or at mininum every processEvery)
476
489
return new Promise ( ( resolve , reject ) =>
477
- setTimeout ( ( ) => {
490
+ setTimeout ( async ( ) => {
478
491
// when job is not running anymore, just finish
479
- if ( ! job . isRunning ( ) ) {
492
+ if ( ! ( await job . isRunning ( ) ) ) {
480
493
resolve ( ) ;
481
494
return ;
482
495
}
0 commit comments