@@ -22,9 +22,16 @@ export class JobProcessor {
22
22
} = { } ;
23
23
24
24
async getStatus ( fullDetails = false ) {
25
+ // eslint-disable-next-line @typescript-eslint/no-var-requires,global-require
26
+ const { version } = require ( '../package.json' ) ;
27
+
25
28
return {
29
+ version,
26
30
queueSize : await this . agenda . db . getQueueSize ( ) ,
27
- jobStatus : this . jobStatus ,
31
+ jobStatus : Object . keys ( this . jobStatus ) . map ( job => ( {
32
+ ...this . jobStatus [ job ] ,
33
+ config : this . agenda . definitions [ job ]
34
+ } ) ) ,
28
35
runningJobs : ! fullDetails ? this . runningJobs . length : this . runningJobs ,
29
36
lockedJobs : ! fullDetails ? this . lockedJobs . length : this . lockedJobs ,
30
37
jobsToLock : ! fullDetails ? this . jobsToLock . length : this . jobsToLock ,
@@ -117,13 +124,13 @@ export class JobProcessor {
117
124
const jobDefinition = this . agenda . definitions [ name ] ;
118
125
let shouldLock = true ;
119
126
// global lock limit
120
- if ( this . totalLockLimit && this . totalLockLimit < = this . lockedJobs . length ) {
127
+ if ( this . totalLockLimit && this . lockedJobs . length > = this . totalLockLimit ) {
121
128
shouldLock = false ;
122
129
}
123
130
124
131
// job specific lock limit
125
132
const status = this . jobStatus [ name ] ;
126
- if ( jobDefinition . lockLimit && status && jobDefinition . lockLimit <= status . locked ) {
133
+ if ( jobDefinition . lockLimit && status && status . locked >= jobDefinition . lockLimit ) {
127
134
shouldLock = false ;
128
135
}
129
136
@@ -188,22 +195,30 @@ export class JobProcessor {
188
195
const resp = await this . agenda . db . lockJob ( job ) ;
189
196
190
197
if ( resp ) {
198
+ if ( job . attrs . name !== resp . name ) {
199
+ throw new Error (
200
+ `got different job name: ${ resp . name } (actual) !== ${ job . attrs . name } (expected)`
201
+ ) ;
202
+ }
203
+
204
+ const jobToEnqueue = new Job ( this . agenda , resp ) ;
205
+
191
206
// Before en-queing job make sure we haven't exceed our lock limits
192
- if ( ! this . shouldLock ( resp . name ) ) {
207
+ if ( ! this . shouldLock ( jobToEnqueue . attrs . name ) ) {
193
208
log (
194
209
'lock limit reached while job was locked in database. Releasing lock on [%s]' ,
195
- resp . name
210
+ jobToEnqueue . attrs . name
196
211
) ;
197
- job . attrs . lockedAt = undefined ;
198
- await job . save ( ) ;
212
+ jobToEnqueue . attrs . lockedAt = undefined ;
213
+ await jobToEnqueue . save ( ) ;
199
214
200
215
this . jobsToLock = [ ] ;
201
216
return ;
202
217
}
203
- const jobToEnqueue = new Job ( this . agenda , resp ) ;
218
+
204
219
log ( 'found job [%s] that can be locked on the fly' , jobToEnqueue . attrs . name ) ;
205
- this . lockedJobs . push ( jobToEnqueue ) ;
206
220
this . updateStatus ( jobToEnqueue . attrs . name , 'locked' , + 1 ) ;
221
+ this . lockedJobs . push ( jobToEnqueue ) ;
207
222
this . enqueueJob ( jobToEnqueue ) ;
208
223
this . jobProcessing ( ) ;
209
224
}
@@ -227,13 +242,12 @@ export class JobProcessor {
227
242
// Find ONE and ONLY ONE job and set the 'lockedAt' time so that job begins to be processed
228
243
const result = await this . agenda . db . getNextJobToRun ( jobName , this . nextScanAt , lockDeadline ) ;
229
244
230
- let job ;
231
245
if ( result ) {
232
246
log ( 'found a job available to lock, creating a new job on Agenda with id [%s]' , result . _id ) ;
233
- job = new Job ( this . agenda , result ) ;
247
+ return new Job ( this . agenda , result ) ;
234
248
}
235
249
236
- return job ;
250
+ return undefined ;
237
251
}
238
252
239
253
/**
@@ -255,12 +269,19 @@ export class JobProcessor {
255
269
// For this job name, find the next job to run and lock it!
256
270
try {
257
271
const job = await this . findAndLockNextJob ( name , this . agenda . definitions [ name ] ) ;
272
+
258
273
// Still have the job?
259
274
// 1. Add it to lock list
260
275
// 2. Add count of locked jobs
261
276
// 3. Queue the job to actually be run now that it is locked
262
277
// 4. Recursively run this same method we are in to check for more available jobs of same type!
263
278
if ( job ) {
279
+ if ( job . attrs . name !== name ) {
280
+ throw new Error (
281
+ `got different job name: ${ job . attrs . name } (acutal) !== ${ name } (expected)`
282
+ ) ;
283
+ }
284
+
264
285
// Before en-queing job make sure we haven't exceed our lock limits
265
286
if ( ! this . shouldLock ( name ) ) {
266
287
log ( 'lock limit reached before job was returned. Releasing lock on [%s]' , name ) ;
@@ -270,8 +291,8 @@ export class JobProcessor {
270
291
}
271
292
272
293
log ( '[%s:%s] job locked while filling queue' , name , job . attrs . _id ) ;
294
+ this . updateStatus ( name , 'locked' , + 1 ) ;
273
295
this . lockedJobs . push ( job ) ;
274
- this . updateStatus ( job . attrs . name , 'locked' , + 1 ) ;
275
296
276
297
this . enqueueJob ( job ) ;
277
298
await this . jobQueueFilling ( name ) ;
@@ -447,8 +468,8 @@ export class JobProcessor {
447
468
running : 0
448
469
} ;
449
470
}
450
- if ( ( this . jobStatus [ name ] ! [ key ] > 0 && number === - 1 ) || number === 1 ) {
451
- this . jobStatus [ name ] ! [ key ] += number ;
452
- }
471
+ // if ((this.jobStatus[name]![key] > 0 && number === -1) || number === 1) {
472
+ this . jobStatus [ name ] ! [ key ] += number ;
473
+ // }
453
474
}
454
475
}
0 commit comments