qdone 2.0.26-alpha → 2.0.27-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.
@@ -168,6 +168,9 @@ function processMessages(queues, callback, options) {
168
168
  return __generator(this, function (_a) {
169
169
  switch (_a.label) {
170
170
  case 0:
171
+ if (opt.verbose) {
172
+ console.error(chalk_1.default.blue('Listening on: '), qname);
173
+ }
171
174
  activeQrls.add(qrl);
172
175
  maxReturnCount += maxMessages;
173
176
  _a.label = 1;
@@ -176,16 +179,18 @@ function processMessages(queues, callback, options) {
176
179
  return [4 /*yield*/, getMessages(qrl, opt, maxMessages)];
177
180
  case 2:
178
181
  messages = _a.sent();
179
- if (messages.length) {
180
- for (_i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
181
- message = messages_1[_i];
182
- jobExecutor.executeJob(message, callback, qname, qrl, function () { return queueManager.updateIcehouse(qrl, true); });
182
+ if (!shutdownRequested) {
183
+ if (messages.length) {
184
+ for (_i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
185
+ message = messages_1[_i];
186
+ jobExecutor.executeJob(message, callback, qname, qrl, function () { return queueManager.updateIcehouse(qrl, true); });
187
+ }
188
+ queueManager.updateIcehouse(qrl, false);
189
+ }
190
+ else {
191
+ // If we didn't get any, update the icehouse so we can back off
192
+ queueManager.updateIcehouse(qrl, true);
183
193
  }
184
- queueManager.updateIcehouse(qrl, false);
185
- }
186
- else {
187
- // If we didn't get any, update the icehouse so we can back off
188
- queueManager.updateIcehouse(qrl, true);
189
194
  }
190
195
  // Max job accounting
191
196
  maxReturnCount -= maxMessages;
@@ -225,7 +230,9 @@ function processMessages(queues, callback, options) {
225
230
  overallFactor = Math.min(latencyFactor, freememFactor, loadFactor);
226
231
  targetJobs = Math.round(allowedJobs * overallFactor);
227
232
  jobsLeft = targetJobs;
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 });
233
+ if (opt.verbose) {
234
+ console.error({ 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 });
235
+ }
229
236
  for (_i = 0, _a = queueManager.getPairs(); _i < _a.length; _i++) {
230
237
  _b = _a[_i], qname = _b.qname, qrl = _b.qrl;
231
238
  // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
@@ -234,9 +241,6 @@ function processMessages(queues, callback, options) {
234
241
  maxMessages = Math.min(10, jobsLeft);
235
242
  listen(qname, qrl, maxMessages);
236
243
  jobsLeft -= maxMessages;
237
- if (opt.verbose) {
238
- console.error(chalk_1.default.blue('Listening on: '), qname);
239
- }
240
244
  // debug({ listenedTo: { qname, maxMessages, jobsLeft } })
241
245
  }
242
246
  return [4 /*yield*/, delay(1000)];
@@ -74,6 +74,9 @@ var JobExecutor = /** @class */ (function () {
74
74
  this.shutdownRequested = true;
75
75
  // Trigger a maintenance run right away in case it speeds us up
76
76
  clearTimeout(this.maintainVisibilityTimeout);
77
+ if (this.opt.verbose) {
78
+ console.error(chalk_1.default.blue('Shutting down jobExecutor'));
79
+ }
77
80
  return [4 /*yield*/, this.maintainPromise];
78
81
  case 1:
79
82
  _a.sent();
@@ -93,12 +96,24 @@ var JobExecutor = /** @class */ (function () {
93
96
  */
94
97
  JobExecutor.prototype.maintainVisibility = function () {
95
98
  return __awaiter(this, void 0, void 0, function () {
96
- var start, jobsToExtendByQrl, jobsToDeleteByQrl, jobsToCleanup, i, job, jobRunTime, jobsToDelete, jobsToExtend, doubled, secondsUntilMax, _a, _b, _c, _i, qrl, jobsToExtend, entries, messageId, job, entry, input, result, count, _d, _e, _f, _g, qrl, jobsToDelete, entries, messageId, job, entry, input, result, count, msElapsed, msPeriod, msLeft, msMin, nextCheckInMs;
99
+ var nextCheckInMs, start, jobsToExtendByQrl, jobsToDeleteByQrl, jobsToCleanup, i, job, jobRunTime, jobsToDelete, jobsToExtend, doubled, secondsUntilMax, _a, _b, _c, _i, qrl, jobsToExtend, entries, messageId, job, entry, input, result, count, _d, _e, _f, _g, qrl, jobsToDelete, entries, messageId, job, entry, input, result, count;
97
100
  var _this = this;
98
101
  return __generator(this, function (_h) {
99
102
  switch (_h.label) {
100
103
  case 0:
104
+ // Bail if we are shutting down
105
+ if (this.shutdownRequested && this.stats.activeJobs === 0 && this.jobs.length === 0) {
106
+ if (this.opt.verbose) {
107
+ console.error(chalk_1.default.blue('All workers done, finishing shutdown of jobExecutor'));
108
+ }
109
+ return [2 /*return*/];
110
+ }
111
+ // Reset our timeout
101
112
  clearTimeout(this.maintainVisibilityTimeout);
113
+ nextCheckInMs = this.shutdownRequested ? 1000 : 10 * 1000;
114
+ this.maintainVisibilityTimeout = setTimeout(function () {
115
+ _this.maintainPromise = _this.maintainVisibility();
116
+ }, nextCheckInMs);
102
117
  start = new Date();
103
118
  jobsToExtendByQrl = {};
104
119
  jobsToDeleteByQrl = {};
@@ -249,18 +264,6 @@ var JobExecutor = /** @class */ (function () {
249
264
  return true;
250
265
  }
251
266
  });
252
- // Bail if we are shutting down
253
- if (this.shutdownRequested && this.stats.activeJobs === 0 && this.jobs.length === 0)
254
- return [2 /*return*/];
255
- msElapsed = new Date() - start;
256
- msPeriod = this.shutdownRequested ? 1 * 1000 : 10 * 1000;
257
- msLeft = Math.max(0, msPeriod - msElapsed);
258
- msMin = this.shutdownRequested ? 1000 : 0;
259
- nextCheckInMs = Math.max(msMin, msLeft);
260
- debug({ msElapsed: msElapsed, msPeriod: msPeriod, msLeft: msLeft, msMin: msMin, nextCheckInMs: nextCheckInMs });
261
- this.maintainVisibilityTimeout = setTimeout(function () {
262
- _this.maintainPromise = _this.maintainVisibility();
263
- }, nextCheckInMs);
264
267
  return [2 /*return*/];
265
268
  }
266
269
  });
@@ -272,6 +275,8 @@ var JobExecutor = /** @class */ (function () {
272
275
  return __generator(this, function (_a) {
273
276
  switch (_a.label) {
274
277
  case 0:
278
+ if (this.shutdownRequested)
279
+ throw new Error('jobExecutor is shutting down so cannot execute new job');
275
280
  payload = this.opt.json ? JSON.parse(message.Body) : message.Body;
276
281
  visibilityTimeout = 60;
277
282
  job = {
@@ -114,6 +114,12 @@ var QueueManager = /** @class */ (function () {
114
114
  clearTimeout(this.resolveTimeout);
115
115
  if (this.shutdownRequested)
116
116
  return [2 /*return*/];
117
+ this.resolveTimeout = setTimeout(function () {
118
+ _this.resolvePromise = _this.resolveQueues();
119
+ }, this.resolveSeconds * 1000);
120
+ if (this.opt.verbose) {
121
+ console.error(chalk_1.default.blue('Will resolve queues again in ' + this.resolveSeconds + ' seconds'));
122
+ }
117
123
  qnames = this.queues.map(function (queue) { return (0, qrlCache_js_1.normalizeQueueName)(queue, _this.opt); });
118
124
  return [4 /*yield*/, (0, qrlCache_js_1.getQnameUrlPairs)(qnames, this.opt)];
119
125
  case 1:
@@ -183,15 +189,9 @@ var QueueManager = /** @class */ (function () {
183
189
  debug('selectedPairs', this.selectedPairs);
184
190
  // Finished resolving
185
191
  if (this.opt.verbose) {
186
- console.error(chalk_1.default.blue(' done'));
192
+ console.error(chalk_1.default.blue('Done resolving'));
187
193
  console.error();
188
194
  }
189
- if (this.opt.verbose) {
190
- console.error(chalk_1.default.blue('Will resolve queues again in ' + this.resolveSeconds + ' seconds'));
191
- }
192
- this.resolveTimeout = setTimeout(function () {
193
- _this.resolvePromise = _this.resolveQueues();
194
- }, this.resolveSeconds * 1000);
195
195
  return [2 /*return*/];
196
196
  }
197
197
  });
@@ -218,6 +218,9 @@ var QueueManager = /** @class */ (function () {
218
218
  case 0:
219
219
  this.shutdownRequested = true;
220
220
  clearTimeout(this.resolveTimeout);
221
+ if (this.opt.verbose) {
222
+ console.error(chalk_1.default.blue('Waiting for queues to resolve'));
223
+ }
221
224
  return [4 /*yield*/, this.resolvePromise];
222
225
  case 1:
223
226
  _a.sent();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qdone",
3
- "version": "2.0.26-alpha",
3
+ "version": "2.0.27-alpha",
4
4
  "description": "Language agnostic job queue for SQS",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/consumer.js CHANGED
@@ -86,18 +86,24 @@ export async function processMessages (queues, callback, options) {
86
86
  const activeQrls = new Set()
87
87
  let maxReturnCount = 0
88
88
  const listen = async (qname, qrl, maxMessages) => {
89
+ if (opt.verbose) {
90
+ console.error(chalk.blue('Listening on: '), qname)
91
+ }
89
92
  activeQrls.add(qrl)
90
93
  maxReturnCount += maxMessages
91
94
  try {
92
95
  const messages = await getMessages(qrl, opt, maxMessages)
93
- if (messages.length) {
94
- for (const message of messages) {
95
- jobExecutor.executeJob(message, callback, qname, qrl, () => queueManager.updateIcehouse(qrl, true))
96
+
97
+ if (!shutdownRequested) {
98
+ if (messages.length) {
99
+ for (const message of messages) {
100
+ jobExecutor.executeJob(message, callback, qname, qrl, () => queueManager.updateIcehouse(qrl, true))
101
+ }
102
+ queueManager.updateIcehouse(qrl, false)
103
+ } else {
104
+ // If we didn't get any, update the icehouse so we can back off
105
+ queueManager.updateIcehouse(qrl, true)
96
106
  }
97
- queueManager.updateIcehouse(qrl, false)
98
- } else {
99
- // If we didn't get any, update the icehouse so we can back off
100
- queueManager.updateIcehouse(qrl, true)
101
107
  }
102
108
 
103
109
  // Max job accounting
@@ -138,16 +144,16 @@ export async function processMessages (queues, callback, options) {
138
144
  const overallFactor = Math.min(latencyFactor, freememFactor, loadFactor)
139
145
  const targetJobs = Math.round(allowedJobs * overallFactor)
140
146
  let jobsLeft = targetJobs
141
- debug({ jobCount: jobExecutor.activeJobCount(), freeMemory, totalMemory, freememThreshold, remainingMemory, memoryThreshold, maxReturnCount, allowedJobs, maxLatency, latency, latencyFactor, freememFactor, oneMinuteLoad, loadPerCore, loadFactor, overallFactor, targetJobs, activeQrls })
147
+
148
+ if (opt.verbose) {
149
+ console.error({ jobCount: jobExecutor.activeJobCount(), freeMemory, totalMemory, freememThreshold, remainingMemory, memoryThreshold, maxReturnCount, allowedJobs, maxLatency, latency, latencyFactor, freememFactor, oneMinuteLoad, loadPerCore, loadFactor, overallFactor, targetJobs, activeQrls })
150
+ }
142
151
  for (const { qname, qrl } of queueManager.getPairs()) {
143
152
  // debug({ evaluating: { qname, qrl, jobsLeft, activeQrlsHasQrl: activeQrls.has(qrl) } })
144
153
  if (jobsLeft <= 0 || activeQrls.has(qrl)) continue
145
154
  const maxMessages = Math.min(10, jobsLeft)
146
155
  listen(qname, qrl, maxMessages)
147
156
  jobsLeft -= maxMessages
148
- if (opt.verbose) {
149
- console.error(chalk.blue('Listening on: '), qname)
150
- }
151
157
  // debug({ listenedTo: { qname, maxMessages, jobsLeft } })
152
158
  }
153
159
  await delay(1000)
@@ -35,6 +35,9 @@ export class JobExecutor {
35
35
  this.shutdownRequested = true
36
36
  // Trigger a maintenance run right away in case it speeds us up
37
37
  clearTimeout(this.maintainVisibilityTimeout)
38
+ if (this.opt.verbose) {
39
+ console.error(chalk.blue('Shutting down jobExecutor'))
40
+ }
38
41
  await this.maintainPromise
39
42
  await this.maintainVisibility()
40
43
  }
@@ -47,7 +50,21 @@ export class JobExecutor {
47
50
  * Changes message visibility on all running jobs using as few calls as possible.
48
51
  */
49
52
  async maintainVisibility () {
53
+ // Bail if we are shutting down
54
+ if (this.shutdownRequested && this.stats.activeJobs === 0 && this.jobs.length === 0) {
55
+ if (this.opt.verbose) {
56
+ console.error(chalk.blue('All workers done, finishing shutdown of jobExecutor'))
57
+ }
58
+ return
59
+ }
60
+
61
+ // Reset our timeout
50
62
  clearTimeout(this.maintainVisibilityTimeout)
63
+ const nextCheckInMs = this.shutdownRequested ? 1000 : 10 * 1000
64
+ this.maintainVisibilityTimeout = setTimeout(() => {
65
+ this.maintainPromise = this.maintainVisibility()
66
+ }, nextCheckInMs)
67
+
51
68
  // debug('maintainVisibility', this.jobs)
52
69
  const start = new Date()
53
70
  const jobsToExtendByQrl = {}
@@ -176,24 +193,10 @@ export class JobExecutor {
176
193
  return true
177
194
  }
178
195
  })
179
-
180
- // Bail if we are shutting down
181
- if (this.shutdownRequested && this.stats.activeJobs === 0 && this.jobs.length === 0) return
182
-
183
- // Check later, but count the time we spent. Make sure we check at least
184
- // every period seconds.
185
- const msElapsed = new Date() - start
186
- const msPeriod = this.shutdownRequested ? 1 * 1000 : 10 * 1000
187
- const msLeft = Math.max(0, msPeriod - msElapsed)
188
- const msMin = this.shutdownRequested ? 1000 : 0
189
- const nextCheckInMs = Math.max(msMin, msLeft)
190
- debug({ msElapsed, msPeriod, msLeft, msMin, nextCheckInMs })
191
- this.maintainVisibilityTimeout = setTimeout(() => {
192
- this.maintainPromise = this.maintainVisibility()
193
- }, nextCheckInMs)
194
196
  }
195
197
 
196
198
  async executeJob (message, callback, qname, qrl, failedCallback) {
199
+ if (this.shutdownRequested) throw new Error('jobExecutor is shutting down so cannot execute new job')
197
200
  // Create job entry and track it
198
201
  const payload = this.opt.json ? JSON.parse(message.Body) : message.Body
199
202
  const visibilityTimeout = 60
@@ -69,6 +69,12 @@ export class QueueManager {
69
69
  async resolveQueues () {
70
70
  clearTimeout(this.resolveTimeout)
71
71
  if (this.shutdownRequested) return
72
+ this.resolveTimeout = setTimeout(() => {
73
+ this.resolvePromise = this.resolveQueues()
74
+ }, this.resolveSeconds * 1000)
75
+ if (this.opt.verbose) {
76
+ console.error(chalk.blue('Will resolve queues again in ' + this.resolveSeconds + ' seconds'))
77
+ }
72
78
 
73
79
  // Start processing
74
80
  const qnames = this.queues.map(queue => normalizeQueueName(queue, this.opt))
@@ -120,16 +126,9 @@ export class QueueManager {
120
126
 
121
127
  // Finished resolving
122
128
  if (this.opt.verbose) {
123
- console.error(chalk.blue(' done'))
129
+ console.error(chalk.blue('Done resolving'))
124
130
  console.error()
125
131
  }
126
-
127
- if (this.opt.verbose) {
128
- console.error(chalk.blue('Will resolve queues again in ' + this.resolveSeconds + ' seconds'))
129
- }
130
- this.resolveTimeout = setTimeout(() => {
131
- this.resolvePromise = this.resolveQueues()
132
- }, this.resolveSeconds * 1000)
133
132
  }
134
133
 
135
134
  // Return the next queue in the lineup
@@ -147,6 +146,9 @@ export class QueueManager {
147
146
  async shutdown () {
148
147
  this.shutdownRequested = true
149
148
  clearTimeout(this.resolveTimeout)
149
+ if (this.opt.verbose) {
150
+ console.error(chalk.blue('Waiting for queues to resolve'))
151
+ }
150
152
  await this.resolvePromise
151
153
  }
152
154
  }