qdone 2.0.22-alpha → 2.0.24-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, latency, latencyFactor, freememFactor, targetJobs, jobsLeft, _i, _a, _b, qname, qrl, maxMessages;
121
+ var opt, lastLatency, systemMonitor, jobExecutor, queueManager, delayTimeout, delay, activeQrls, maxReturnCount, listen, allowedJobs, maxLatency, freeMemory, totalMemory, remainingMemory, memoryThreshold, latency, latencyFactor, freememFactor, 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) {
@@ -178,7 +178,7 @@ function processMessages(queues, callback, options) {
178
178
  if (messages.length) {
179
179
  for (_i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
180
180
  message = messages_1[_i];
181
- jobExecutor.executeJob(message, callback, qname, qrl);
181
+ jobExecutor.executeJob(message, callback, qname, qrl, function () { return queueManager.updateIcehouse(qrl, true); });
182
182
  }
183
183
  queueManager.updateIcehouse(qrl, false);
184
184
  }
@@ -211,14 +211,15 @@ function processMessages(queues, callback, options) {
211
211
  maxLatency = 100;
212
212
  freeMemory = (0, os_1.freemem)();
213
213
  totalMemory = (0, os_1.totalmem)();
214
+ remainingMemory = totalMemory - freeMemory;
214
215
  memoryThreshold = totalMemory * opt.maxMemoryPercent / 100;
215
216
  latency = systemMonitor.getLatency() || 10;
216
217
  latencyFactor = 1 - Math.abs(Math.min(latency / maxLatency, 1)) // 0 if latency is at max, 1 if latency 0
217
218
  ;
218
- freememFactor = Math.min(1, Math.max(0, freeMemory / memoryThreshold));
219
+ freememFactor = Math.min(1, Math.max(0, remainingMemory / memoryThreshold));
219
220
  targetJobs = Math.round(allowedJobs * latencyFactor * freememFactor);
220
221
  jobsLeft = targetJobs;
221
- debug({ jobCount: jobExecutor.activeJobCount(), freeMemory: freeMemory, totalMemory: totalMemory, memoryThreshold: memoryThreshold, maxReturnCount: maxReturnCount, allowedJobs: allowedJobs, maxLatency: maxLatency, latency: latency, latencyFactor: latencyFactor, freememFactor: freememFactor, targetJobs: targetJobs, activeQrls: activeQrls });
222
+ debug({ jobCount: jobExecutor.activeJobCount(), freeMemory: freeMemory, totalMemory: totalMemory, remainingMemory: remainingMemory, memoryThreshold: memoryThreshold, maxReturnCount: maxReturnCount, allowedJobs: allowedJobs, maxLatency: maxLatency, latency: latency, latencyFactor: latencyFactor, freememFactor: freememFactor, targetJobs: targetJobs, activeQrls: activeQrls });
222
223
  for (_i = 0, _a = queueManager.getPairs(); _i < _a.length; _i++) {
223
224
  _b = _a[_i], qname = _b.qname, qrl = _b.qrl;
224
225
  // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
@@ -266,7 +266,7 @@ var JobExecutor = /** @class */ (function () {
266
266
  });
267
267
  });
268
268
  };
269
- JobExecutor.prototype.executeJob = function (message, callback, qname, qrl) {
269
+ JobExecutor.prototype.executeJob = function (message, callback, qname, qrl, failedCallback) {
270
270
  return __awaiter(this, void 0, void 0, function () {
271
271
  var payload, visibilityTimeout, job, oldJob, e, queue, result, err_1;
272
272
  return __generator(this, function (_a) {
@@ -340,6 +340,9 @@ var JobExecutor = /** @class */ (function () {
340
340
  return [3 /*break*/, 4];
341
341
  case 3:
342
342
  err_1 = _a.sent();
343
+ // Notify caller that we failed
344
+ if (failedCallback)
345
+ failedCallback(message, qname, qrl);
343
346
  // Fail path for job execution
344
347
  if (this.opt.verbose) {
345
348
  console.error(chalk_1.default.red('FAILED'), message.Body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qdone",
3
- "version": "2.0.22-alpha",
3
+ "version": "2.0.24-alpha",
4
4
  "description": "Language agnostic job queue for SQS",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/consumer.js CHANGED
@@ -91,7 +91,7 @@ export async function processMessages (queues, callback, options) {
91
91
  const messages = await getMessages(qrl, opt, maxMessages)
92
92
  if (messages.length) {
93
93
  for (const message of messages) {
94
- jobExecutor.executeJob(message, callback, qname, qrl)
94
+ jobExecutor.executeJob(message, callback, qname, qrl, () => queueManager.updateIcehouse(qrl, true))
95
95
  }
96
96
  queueManager.updateIcehouse(qrl, false)
97
97
  } else {
@@ -118,13 +118,14 @@ export async function processMessages (queues, callback, options) {
118
118
  const maxLatency = 100
119
119
  const freeMemory = freemem()
120
120
  const totalMemory = totalmem()
121
+ const remainingMemory = totalMemory - freeMemory
121
122
  const memoryThreshold = totalMemory * opt.maxMemoryPercent / 100
122
123
  const latency = systemMonitor.getLatency() || 10
123
124
  const latencyFactor = 1 - Math.abs(Math.min(latency / maxLatency, 1)) // 0 if latency is at max, 1 if latency 0
124
- const freememFactor = Math.min(1, Math.max(0, freeMemory / memoryThreshold))
125
+ const freememFactor = Math.min(1, Math.max(0, remainingMemory / memoryThreshold))
125
126
  const targetJobs = Math.round(allowedJobs * latencyFactor * freememFactor)
126
127
  let jobsLeft = targetJobs
127
- debug({ jobCount: jobExecutor.activeJobCount(), freeMemory, totalMemory, memoryThreshold, maxReturnCount, allowedJobs, maxLatency, latency, latencyFactor, freememFactor, targetJobs, activeQrls })
128
+ debug({ jobCount: jobExecutor.activeJobCount(), freeMemory, totalMemory, remainingMemory, memoryThreshold, maxReturnCount, allowedJobs, maxLatency, latency, latencyFactor, freememFactor, targetJobs, activeQrls })
128
129
  for (const { qname, qrl } of queueManager.getPairs()) {
129
130
  // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
130
131
  if (jobsLeft <= 0 || activeQrls.has(qrl)) continue
@@ -193,7 +193,7 @@ export class JobExecutor {
193
193
  }, nextCheckInMs)
194
194
  }
195
195
 
196
- async executeJob (message, callback, qname, qrl) {
196
+ async executeJob (message, callback, qname, qrl, failedCallback) {
197
197
  // Create job entry and track it
198
198
  const payload = this.opt.json ? JSON.parse(message.Body) : message.Body
199
199
  const visibilityTimeout = 60
@@ -261,6 +261,8 @@ export class JobExecutor {
261
261
  }
262
262
  this.stats.jobsSucceeded++
263
263
  } catch (err) {
264
+ // Notify caller that we failed
265
+ if (failedCallback) failedCallback(message, qname, qrl)
264
266
  // Fail path for job execution
265
267
  if (this.opt.verbose) {
266
268
  console.error(chalk.red('FAILED'), message.Body)