qdone 2.0.25-alpha → 2.0.26-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.
@@ -118,7 +118,7 @@ exports.getMessages = getMessages;
118
118
  //
119
119
  function processMessages(queues, callback, options) {
120
120
  return __awaiter(this, void 0, void 0, function () {
121
- var opt, lastLatency, systemMonitor, jobExecutor, queueManager, delayTimeout, delay, activeQrls, maxReturnCount, listen, allowedJobs, maxLatency, freeMemory, totalMemory, memoryThreshold, freememThreshold, remainingMemory, latency, latencyFactor, freememFactor, targetJobs, jobsLeft, _i, _a, _b, qname, qrl, maxMessages;
121
+ var opt, lastLatency, systemMonitor, jobExecutor, queueManager, cores, delayTimeout, delay, activeQrls, maxReturnCount, listen, allowedJobs, maxLatency, latency, latencyFactor, freeMemory, totalMemory, memoryThreshold, freememThreshold, remainingMemory, freememFactor, oneMinuteLoad, loadPerCore, loadFactor, overallFactor, targetJobs, jobsLeft, _i, _a, _b, qname, qrl, maxMessages;
122
122
  var _this = this;
123
123
  return __generator(this, function (_c) {
124
124
  switch (_c.label) {
@@ -136,6 +136,7 @@ function processMessages(queues, callback, options) {
136
136
  });
137
137
  jobExecutor = new jobExecutor_js_1.JobExecutor(opt);
138
138
  queueManager = new queueManager_js_1.QueueManager(opt, queues, 60);
139
+ cores = (0, os_1.cpus)().length;
139
140
  delay = function (ms) { return new Promise(function (resolve) {
140
141
  delayTimeout = setTimeout(resolve, ms);
141
142
  }); };
@@ -209,18 +210,22 @@ function processMessages(queues, callback, options) {
209
210
  if (!!shutdownRequested) return [3 /*break*/, 3];
210
211
  allowedJobs = Math.max(0, opt.maxConcurrentJobs - jobExecutor.activeJobCount() - maxReturnCount);
211
212
  maxLatency = 100;
213
+ latency = systemMonitor.getLatency() || 10;
214
+ latencyFactor = 1 - Math.abs(Math.min(latency / maxLatency, 1)) // 0 if latency is at max, 1 if latency 0
215
+ ;
212
216
  freeMemory = (0, os_1.freemem)();
213
217
  totalMemory = (0, os_1.totalmem)();
214
218
  memoryThreshold = totalMemory * opt.maxMemoryPercent / 100;
215
219
  freememThreshold = totalMemory - memoryThreshold;
216
220
  remainingMemory = Math.max(0, freeMemory - freememThreshold);
217
- latency = systemMonitor.getLatency() || 10;
218
- latencyFactor = 1 - Math.abs(Math.min(latency / maxLatency, 1)) // 0 if latency is at max, 1 if latency 0
219
- ;
220
221
  freememFactor = Math.min(1, Math.max(0, remainingMemory / memoryThreshold));
221
- targetJobs = Math.round(allowedJobs * latencyFactor * freememFactor);
222
+ oneMinuteLoad = (0, os_1.loadavg)()[0];
223
+ loadPerCore = oneMinuteLoad / cores;
224
+ loadFactor = 1 - Math.min(1, Math.max(0, loadPerCore / 3));
225
+ overallFactor = Math.min(latencyFactor, freememFactor, loadFactor);
226
+ targetJobs = Math.round(allowedJobs * overallFactor);
222
227
  jobsLeft = targetJobs;
223
- debug({ jobCount: jobExecutor.activeJobCount(), freeMemory: freeMemory, totalMemory: totalMemory, freememThreshold: freememThreshold, remainingMemory: remainingMemory, memoryThreshold: memoryThreshold, maxReturnCount: maxReturnCount, allowedJobs: allowedJobs, maxLatency: maxLatency, latency: latency, latencyFactor: latencyFactor, freememFactor: freememFactor, targetJobs: targetJobs, activeQrls: activeQrls });
228
+ debug({ jobCount: jobExecutor.activeJobCount(), freeMemory: freeMemory, totalMemory: totalMemory, freememThreshold: freememThreshold, remainingMemory: remainingMemory, memoryThreshold: memoryThreshold, maxReturnCount: maxReturnCount, allowedJobs: allowedJobs, maxLatency: maxLatency, latency: latency, latencyFactor: latencyFactor, freememFactor: freememFactor, oneMinuteLoad: oneMinuteLoad, loadPerCore: loadPerCore, loadFactor: loadFactor, overallFactor: overallFactor, targetJobs: targetJobs, activeQrls: activeQrls });
224
229
  for (_i = 0, _a = queueManager.getPairs(); _i < _a.length; _i++) {
225
230
  _b = _a[_i], qname = _b.qname, qrl = _b.qrl;
226
231
  // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qdone",
3
- "version": "2.0.25-alpha",
3
+ "version": "2.0.26-alpha",
4
4
  "description": "Language agnostic job queue for SQS",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/consumer.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Consumer implementation.
3
3
  */
4
4
 
5
- import { freemem, totalmem } from 'os'
5
+ import { freemem, totalmem, loadavg, cpus } from 'os'
6
6
  import { ReceiveMessageCommand, QueueDoesNotExist } from '@aws-sdk/client-sqs'
7
7
  import chalk from 'chalk'
8
8
  import Debug from 'debug'
@@ -62,6 +62,7 @@ export async function processMessages (queues, callback, options) {
62
62
  })
63
63
  const jobExecutor = new JobExecutor(opt)
64
64
  const queueManager = new QueueManager(opt, queues, 60)
65
+ const cores = cpus().length
65
66
  // debug({ systemMonitor, jobExecutor, queueManager })
66
67
 
67
68
  // This delay function keeps a timeout reference around so it can be
@@ -115,18 +116,29 @@ export async function processMessages (queues, callback, options) {
115
116
  while (!shutdownRequested) { // eslint-disable-line
116
117
  // Figure out how we are running
117
118
  const allowedJobs = Math.max(0, opt.maxConcurrentJobs - jobExecutor.activeJobCount() - maxReturnCount)
119
+
120
+ // Latency
118
121
  const maxLatency = 100
122
+ const latency = systemMonitor.getLatency() || 10
123
+ const latencyFactor = 1 - Math.abs(Math.min(latency / maxLatency, 1)) // 0 if latency is at max, 1 if latency 0
124
+
125
+ // Memory
119
126
  const freeMemory = freemem()
120
127
  const totalMemory = totalmem()
121
128
  const memoryThreshold = totalMemory * opt.maxMemoryPercent / 100
122
129
  const freememThreshold = totalMemory - memoryThreshold
123
130
  const remainingMemory = Math.max(0, freeMemory - freememThreshold)
124
- const latency = systemMonitor.getLatency() || 10
125
- const latencyFactor = 1 - Math.abs(Math.min(latency / maxLatency, 1)) // 0 if latency is at max, 1 if latency 0
126
131
  const freememFactor = Math.min(1, Math.max(0, remainingMemory / memoryThreshold))
127
- const targetJobs = Math.round(allowedJobs * latencyFactor * freememFactor)
132
+
133
+ // Load
134
+ const oneMinuteLoad = loadavg()[0]
135
+ const loadPerCore = oneMinuteLoad / cores
136
+ const loadFactor = 1 - Math.min(1, Math.max(0, loadPerCore / 3))
137
+
138
+ const overallFactor = Math.min(latencyFactor, freememFactor, loadFactor)
139
+ const targetJobs = Math.round(allowedJobs * overallFactor)
128
140
  let jobsLeft = targetJobs
129
- debug({ jobCount: jobExecutor.activeJobCount(), freeMemory, totalMemory, freememThreshold, remainingMemory, memoryThreshold, maxReturnCount, allowedJobs, maxLatency, latency, latencyFactor, freememFactor, targetJobs, activeQrls })
141
+ debug({ jobCount: jobExecutor.activeJobCount(), freeMemory, totalMemory, freememThreshold, remainingMemory, memoryThreshold, maxReturnCount, allowedJobs, maxLatency, latency, latencyFactor, freememFactor, oneMinuteLoad, loadPerCore, loadFactor, overallFactor, targetJobs, activeQrls })
130
142
  for (const { qname, qrl } of queueManager.getPairs()) {
131
143
  // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
132
144
  if (jobsLeft <= 0 || activeQrls.has(qrl)) continue