qdone 2.0.30-alpha → 2.0.31-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qdone",
3
- "version": "2.0.30-alpha",
3
+ "version": "2.0.31-alpha",
4
4
  "description": "Language agnostic job queue for SQS",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/consumer.js CHANGED
@@ -119,7 +119,7 @@ export async function processMessages (queues, callback, options) {
119
119
 
120
120
  while (!shutdownRequested) { // eslint-disable-line
121
121
  // Figure out how we are running
122
- const allowedJobs = Math.max(0, opt.maxConcurrentJobs - jobExecutor.activeJobCount() - maxReturnCount)
122
+ const allowedJobs = Math.max(0, opt.maxConcurrentJobs - jobExecutor.runningJobCount() - maxReturnCount)
123
123
 
124
124
  // Latency
125
125
  const maxLatency = 100
@@ -147,7 +147,8 @@ export async function processMessages (queues, callback, options) {
147
147
  // console.error({ maxConcurrentJobs: opt.maxConcurrentJobs, jobCount: jobExecutor.activeJobCount(), allowedJobs, maxLatency, latencyFactor, freememFactor, loadFactor, overallFactor, targetJobs, activeQrls })
148
148
  }
149
149
  for (const { qname, qrl } of queueManager.getPairs()) {
150
- // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
150
+ // const qcount = jobExecutor.runningJobCountForQueue(qname)
151
+ // console.log({ evaluating: { qname, qrl, qcount, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
151
152
  if (jobsLeft <= 0 || activeQrls.has(qrl)) continue
152
153
  const maxMessages = Math.min(10, jobsLeft)
153
154
  listen(qname, qrl, maxMessages)
@@ -19,6 +19,7 @@ export class JobExecutor {
19
19
  this.opt = opt
20
20
  this.jobs = []
21
21
  this.jobsByMessageId = {}
22
+ this.jobsByQueue = new Map()
22
23
  this.stats = {
23
24
  activeJobs: 0,
24
25
  waitingJobs: 0,
@@ -48,6 +49,20 @@ export class JobExecutor {
48
49
  return this.stats.activeJobs
49
50
  }
50
51
 
52
+ runningJobCount () {
53
+ return this.stats.runningJobs
54
+ }
55
+
56
+ /**
57
+ * Returns the number of jobs running in a queue.
58
+ */
59
+ runningJobCountForQueue (qname) {
60
+ const jobs = this.jobsByQueue.get(qname) || new Set()
61
+ let runningCount = 0
62
+ for (const job of jobs.values()) runningCount += job.status === 'running'
63
+ return runningCount
64
+ }
65
+
51
66
  /**
52
67
  * Changes message visibility on all running jobs using as few calls as possible.
53
68
  */
@@ -203,7 +218,9 @@ export class JobExecutor {
203
218
  this.jobs = this.jobs.filter(job => {
204
219
  if (job.status === 'deleting' || job.status === 'failed') {
205
220
  debug('removed', job.message.MessageId)
221
+ // Accounting
206
222
  delete this.jobsByMessageId[job.message.MessageId]
223
+ this.jobsByQueue.get(job.qname).delete(job)
207
224
  return false
208
225
  } else {
209
226
  return true
@@ -240,8 +257,14 @@ export class JobExecutor {
240
257
  throw e
241
258
  }
242
259
 
260
+ // Accounting
243
261
  this.jobs.push(job)
244
262
  this.jobsByMessageId[job.message.MessageId] = job
263
+
264
+ // Track all jobs for each queue
265
+ if (!this.jobsByQueue.has(job.qname)) this.jobsByQueue.set(job.qname, new Set())
266
+ this.jobsByQueue.get(job.qname).add(job)
267
+
245
268
  this.stats.activeJobs++
246
269
  this.stats.waitingJobs++
247
270
  if (this.opt.verbose) {
@@ -332,13 +355,10 @@ export class JobExecutor {
332
355
  // Begin executing
333
356
  for (const [job, i] of jobs.map((job, i) => [job, i])) {
334
357
  // Figure out if the next job needs to happen in serial, otherwise we can parallel execute
335
- // const job = jobs[i]
336
358
  const nextJob = jobs[i + 1]
337
- const nextJobIsSerial =
338
- isFifo && nextJob &&
339
- job.message?.Attributes?.GroupId === nextJob.message?.Attributes?.GroupId
359
+ const nextJobIsSerial = isFifo && nextJob && job.message?.Attributes?.GroupId === nextJob.message?.Attributes?.GroupId
340
360
 
341
- console.log({ i, nextJobAtt: nextJob.message.Attributes, nextJobIsSerial })
361
+ // console.log({ i, nextJobAtt: nextJob?.message?.Attributes, nextJobIsSerial })
342
362
  // Execute serial or parallel
343
363
  if (nextJobIsSerial) await this.runJob(job)
344
364
  else this.runJob(job)