qdone 2.0.26-alpha → 2.0.28-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, _d, _e, failed, count, _f, _g, _h, _j, qrl, jobsToDelete, entries, messageId, job, entry, input, result, _k, _l, failed, count;
97
100
  var _this = this;
98
- return __generator(this, function (_h) {
99
- switch (_h.label) {
101
+ return __generator(this, function (_m) {
102
+ switch (_m.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 = {};
@@ -134,7 +149,7 @@ var JobExecutor = /** @class */ (function () {
134
149
  secondsUntilMax = Math.max(1, maxJobSeconds - jobRunTime);
135
150
  // const secondsUntilKill = Math.max(1, this.opt.killAfter - jobRunTime)
136
151
  job.visibilityTimeout = Math.min(doubled, secondsUntilMax); //, secondsUntilKill)
137
- job.extendAtSecond = Math.round(jobRunTime + job.visibilityTimeout); // this is what we use next time
152
+ job.extendAtSecond = Math.round(jobRunTime + job.visibilityTimeout / 2); // this is what we use next time
138
153
  debug({ doubled: doubled, secondsUntilMax: secondsUntilMax, job: job });
139
154
  }
140
155
  }
@@ -144,7 +159,7 @@ var JobExecutor = /** @class */ (function () {
144
159
  for (_c in _a)
145
160
  _b.push(_c);
146
161
  _i = 0;
147
- _h.label = 1;
162
+ _m.label = 1;
148
163
  case 1:
149
164
  if (!(_i < _b.length)) return [3 /*break*/, 5];
150
165
  _c = _b[_i];
@@ -152,7 +167,7 @@ var JobExecutor = /** @class */ (function () {
152
167
  qrl = _c;
153
168
  jobsToExtend = jobsToExtendByQrl[qrl];
154
169
  debug({ qrl: qrl, jobsToExtend: jobsToExtend });
155
- _h.label = 2;
170
+ _m.label = 2;
156
171
  case 2:
157
172
  if (!jobsToExtend.length) return [3 /*break*/, 4];
158
173
  entries = [];
@@ -160,7 +175,7 @@ var JobExecutor = /** @class */ (function () {
160
175
  while (messageId++ < 10 && jobsToExtend.length) {
161
176
  job = jobsToExtend.shift();
162
177
  entry = {
163
- Id: '' + messageId,
178
+ Id: job.message.MessageId,
164
179
  ReceiptHandle: job.message.ReceiptHandle,
165
180
  VisibilityTimeout: job.visibilityTimeout
166
181
  };
@@ -171,9 +186,16 @@ var JobExecutor = /** @class */ (function () {
171
186
  debug({ ChangeMessageVisibilityBatch: input });
172
187
  return [4 /*yield*/, (0, sqs_js_1.getSQSClient)().send(new client_sqs_1.ChangeMessageVisibilityBatchCommand(input))];
173
188
  case 3:
174
- result = _h.sent();
189
+ result = _m.sent();
175
190
  debug('ChangeMessageVisibilityBatch returned', result);
176
191
  this.stats.sqsCalls++;
192
+ if (result.Failed) {
193
+ console.error('FAILED_MESSAGES', result.Failed);
194
+ for (_d = 0, _e = result.Failed; _d < _e.length; _d++) {
195
+ failed = _e[_d];
196
+ console.error('FAILED_TO_EXTEND_JOB', this.jobsByMessageId[failed.Id]);
197
+ }
198
+ }
177
199
  if (result.Successful) {
178
200
  count = result.Successful.length || 0;
179
201
  this.stats.timeoutsExtended += count;
@@ -189,19 +211,19 @@ var JobExecutor = /** @class */ (function () {
189
211
  _i++;
190
212
  return [3 /*break*/, 1];
191
213
  case 5:
192
- _d = jobsToDeleteByQrl;
193
- _e = [];
194
- for (_f in _d)
195
- _e.push(_f);
196
- _g = 0;
197
- _h.label = 6;
214
+ _f = jobsToDeleteByQrl;
215
+ _g = [];
216
+ for (_h in _f)
217
+ _g.push(_h);
218
+ _j = 0;
219
+ _m.label = 6;
198
220
  case 6:
199
- if (!(_g < _e.length)) return [3 /*break*/, 10];
200
- _f = _e[_g];
201
- if (!(_f in _d)) return [3 /*break*/, 9];
202
- qrl = _f;
221
+ if (!(_j < _g.length)) return [3 /*break*/, 10];
222
+ _h = _g[_j];
223
+ if (!(_h in _f)) return [3 /*break*/, 9];
224
+ qrl = _h;
203
225
  jobsToDelete = jobsToDeleteByQrl[qrl];
204
- _h.label = 7;
226
+ _m.label = 7;
205
227
  case 7:
206
228
  if (!jobsToDelete.length) return [3 /*break*/, 9];
207
229
  entries = [];
@@ -209,7 +231,7 @@ var JobExecutor = /** @class */ (function () {
209
231
  while (messageId++ < 10 && jobsToDelete.length) {
210
232
  job = jobsToDelete.shift();
211
233
  entry = {
212
- Id: '' + messageId,
234
+ Id: job.message.MessageId,
213
235
  ReceiptHandle: job.message.ReceiptHandle,
214
236
  VisibilityTimeout: job.visibilityTimeout
215
237
  };
@@ -220,8 +242,15 @@ var JobExecutor = /** @class */ (function () {
220
242
  debug({ DeleteMessageBatch: input });
221
243
  return [4 /*yield*/, (0, sqs_js_1.getSQSClient)().send(new client_sqs_1.DeleteMessageBatchCommand(input))];
222
244
  case 8:
223
- result = _h.sent();
245
+ result = _m.sent();
224
246
  this.stats.sqsCalls++;
247
+ if (result.Failed) {
248
+ console.error('FAILED_MESSAGES', result.Failed);
249
+ for (_k = 0, _l = result.Failed; _k < _l.length; _k++) {
250
+ failed = _l[_k];
251
+ console.error('FAILED_TO_DELETE_JOB', this.jobsByMessageId[failed.Id]);
252
+ }
253
+ }
225
254
  if (result.Successful) {
226
255
  count = result.Successful.length || 0;
227
256
  this.stats.jobsDeleted += count;
@@ -235,7 +264,7 @@ var JobExecutor = /** @class */ (function () {
235
264
  debug('DeleteMessageBatch returned', result);
236
265
  return [3 /*break*/, 7];
237
266
  case 9:
238
- _g++;
267
+ _j++;
239
268
  return [3 /*break*/, 6];
240
269
  case 10:
241
270
  // Get rid of deleted and failed jobs
@@ -249,18 +278,6 @@ var JobExecutor = /** @class */ (function () {
249
278
  return true;
250
279
  }
251
280
  });
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
281
  return [2 /*return*/];
265
282
  }
266
283
  });
@@ -272,6 +289,8 @@ var JobExecutor = /** @class */ (function () {
272
289
  return __generator(this, function (_a) {
273
290
  switch (_a.label) {
274
291
  case 0:
292
+ if (this.shutdownRequested)
293
+ throw new Error('jobExecutor is shutting down so cannot execute new job');
275
294
  payload = this.opt.json ? JSON.parse(message.Body) : message.Body;
276
295
  visibilityTimeout = 60;
277
296
  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();