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.
@@ -117,7 +117,7 @@ async function processMessages(queues, callback, options) {
117
117
  };
118
118
  while (!shutdownRequested) { // eslint-disable-line
119
119
  // Figure out how we are running
120
- const allowedJobs = Math.max(0, opt.maxConcurrentJobs - jobExecutor.activeJobCount() - maxReturnCount);
120
+ const allowedJobs = Math.max(0, opt.maxConcurrentJobs - jobExecutor.runningJobCount() - maxReturnCount);
121
121
  // Latency
122
122
  const maxLatency = 100;
123
123
  const latency = systemMonitor.getLatency() || 10;
@@ -140,7 +140,8 @@ async function processMessages(queues, callback, options) {
140
140
  // console.error({ maxConcurrentJobs: opt.maxConcurrentJobs, jobCount: jobExecutor.activeJobCount(), allowedJobs, maxLatency, latencyFactor, freememFactor, loadFactor, overallFactor, targetJobs, activeQrls })
141
141
  }
142
142
  for (const { qname, qrl } of queueManager.getPairs()) {
143
- // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
143
+ // const qcount = jobExecutor.runningJobCountForQueue(qname)
144
+ // console.log({ evaluating: { qname, qrl, qcount, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
144
145
  if (jobsLeft <= 0 || activeQrls.has(qrl))
145
146
  continue;
146
147
  const maxMessages = Math.min(10, jobsLeft);
@@ -19,6 +19,7 @@ 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,
@@ -45,6 +46,19 @@ class JobExecutor {
45
46
  activeJobCount() {
46
47
  return this.stats.activeJobs;
47
48
  }
49
+ runningJobCount() {
50
+ return this.stats.runningJobs;
51
+ }
52
+ /**
53
+ * Returns the number of jobs running in a queue.
54
+ */
55
+ runningJobCountForQueue(qname) {
56
+ const jobs = this.jobsByQueue.get(qname) || new Set();
57
+ let runningCount = 0;
58
+ for (const job of jobs.values())
59
+ runningCount += job.status === 'running';
60
+ return runningCount;
61
+ }
48
62
  /**
49
63
  * Changes message visibility on all running jobs using as few calls as possible.
50
64
  */
@@ -194,7 +208,9 @@ class JobExecutor {
194
208
  this.jobs = this.jobs.filter(job => {
195
209
  if (job.status === 'deleting' || job.status === 'failed') {
196
210
  debug('removed', job.message.MessageId);
211
+ // Accounting
197
212
  delete this.jobsByMessageId[job.message.MessageId];
213
+ this.jobsByQueue.get(job.qname).delete(job);
198
214
  return false;
199
215
  }
200
216
  else {
@@ -229,8 +245,13 @@ class JobExecutor {
229
245
  // TODO: sentry breadcrumb
230
246
  throw e;
231
247
  }
248
+ // Accounting
232
249
  this.jobs.push(job);
233
250
  this.jobsByMessageId[job.message.MessageId] = job;
251
+ // Track all jobs for each queue
252
+ if (!this.jobsByQueue.has(job.qname))
253
+ this.jobsByQueue.set(job.qname, new Set());
254
+ this.jobsByQueue.get(job.qname).add(job);
234
255
  this.stats.activeJobs++;
235
256
  this.stats.waitingJobs++;
236
257
  if (this.opt.verbose) {
@@ -321,11 +342,9 @@ class JobExecutor {
321
342
  // Begin executing
322
343
  for (const [job, i] of jobs.map((job, i) => [job, i])) {
323
344
  // Figure out if the next job needs to happen in serial, otherwise we can parallel execute
324
- // const job = jobs[i]
325
345
  const nextJob = jobs[i + 1];
326
- const nextJobIsSerial = isFifo && nextJob &&
327
- job.message?.Attributes?.GroupId === nextJob.message?.Attributes?.GroupId;
328
- console.log({ i, nextJobAtt: nextJob.message.Attributes, nextJobIsSerial });
346
+ const nextJobIsSerial = isFifo && nextJob && job.message?.Attributes?.GroupId === nextJob.message?.Attributes?.GroupId;
347
+ // console.log({ i, nextJobAtt: nextJob?.message?.Attributes, nextJobIsSerial })
329
348
  // Execute serial or parallel
330
349
  if (nextJobIsSerial)
331
350
  await this.runJob(job);