qdone 2.0.19-alpha → 2.0.20-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.
package/commonjs/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requestShutdown = exports.processMessages = exports.enqueueBatch = exports.enqueue = void 0;
3
+ exports.monitor = exports.requestShutdown = exports.processMessages = exports.enqueueBatch = exports.enqueue = void 0;
4
4
  var enqueue_js_1 = require("./src/enqueue.js");
5
5
  Object.defineProperty(exports, "enqueue", { enumerable: true, get: function () { return enqueue_js_1.enqueue; } });
6
6
  Object.defineProperty(exports, "enqueueBatch", { enumerable: true, get: function () { return enqueue_js_1.enqueueBatch; } });
7
7
  var consumer_js_1 = require("./src/consumer.js");
8
8
  Object.defineProperty(exports, "processMessages", { enumerable: true, get: function () { return consumer_js_1.processMessages; } });
9
9
  Object.defineProperty(exports, "requestShutdown", { enumerable: true, get: function () { return consumer_js_1.requestShutdown; } });
10
+ var monitor_js_1 = require("./src/monitor.js");
11
+ Object.defineProperty(exports, "monitor", { enumerable: true, get: function () { return monitor_js_1.monitor; } });
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ /**
3
+ * Multi-queue monitoring functionaliry
4
+ */
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ var __generator = (this && this.__generator) || function (thisArg, body) {
15
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
16
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
17
+ function verb(n) { return function (v) { return step([n, v]); }; }
18
+ function step(op) {
19
+ if (f) throw new TypeError("Generator is already executing.");
20
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
21
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
22
+ if (y = 0, t) op = [op[0] & 2, t.value];
23
+ switch (op[0]) {
24
+ case 0: case 1: t = op; break;
25
+ case 4: _.label++; return { value: op[1], done: false };
26
+ case 5: _.label++; y = op[1]; op = [0]; continue;
27
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
28
+ default:
29
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
30
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
31
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
32
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
33
+ if (t[2]) _.ops.pop();
34
+ _.trys.pop(); continue;
35
+ }
36
+ op = body.call(thisArg, _);
37
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
38
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
39
+ }
40
+ };
41
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
42
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
43
+ if (ar || !(i in from)) {
44
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
45
+ ar[i] = from[i];
46
+ }
47
+ }
48
+ return to.concat(ar || Array.prototype.slice.call(from));
49
+ };
50
+ var __importDefault = (this && this.__importDefault) || function (mod) {
51
+ return (mod && mod.__esModule) ? mod : { "default": mod };
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.getAggregateData = exports.interpretWildcard = exports.monitor = void 0;
55
+ var sqs_js_1 = require("./sqs.js");
56
+ var cloudWatch_js_1 = require("./cloudWatch.js");
57
+ var defaults_js_1 = require("./defaults.js");
58
+ var debug_1 = __importDefault(require("debug"));
59
+ var debug = (0, debug_1.default)('sd:utils:qmonitor:index');
60
+ /**
61
+ * Splits a queue name with a single wildcard into prefix and suffix regex.
62
+ */
63
+ function monitor(queue, save, options) {
64
+ return __awaiter(this, void 0, void 0, function () {
65
+ var opt, data;
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0:
69
+ opt = (0, defaults_js_1.getOptionsWithDefaults)(options);
70
+ return [4 /*yield*/, getAggregateData(queue)];
71
+ case 1:
72
+ data = _a.sent();
73
+ console.log(data);
74
+ if (!save) return [3 /*break*/, 3];
75
+ if (opt.verbose)
76
+ process.stderr.write('Saving to CloudWatch...');
77
+ return [4 /*yield*/, (0, cloudWatch_js_1.putAggregateData)(data)];
78
+ case 2:
79
+ _a.sent();
80
+ if (opt.verbose)
81
+ process.stderr.write('done\n');
82
+ _a.label = 3;
83
+ case 3: return [2 /*return*/];
84
+ }
85
+ });
86
+ });
87
+ }
88
+ exports.monitor = monitor;
89
+ /**
90
+ * Splits a queue name with a single wildcard into prefix and suffix regex.
91
+ */
92
+ function interpretWildcard(queueName) {
93
+ var _a = queueName.split('*'), prefix = _a[0], suffix = _a[1];
94
+ // Strip anything that could cause backreferences
95
+ var safeSuffix = (suffix || '').replace(/[^a-zA-Z0-9_.]+/g, '').replace(/\./g, '\\.');
96
+ var suffixRegex = new RegExp("".concat(safeSuffix, "$"));
97
+ // debug({ prefix, suffix, safeSuffix, suffixRegex })
98
+ return { prefix: prefix, suffix: suffix, safeSuffix: safeSuffix, suffixRegex: suffixRegex };
99
+ }
100
+ exports.interpretWildcard = interpretWildcard;
101
+ /**
102
+ * Aggregates inmportant attributes across queues and reports a summary.
103
+ * Attributes:
104
+ * - ApproximateNumberOfMessages: Sum
105
+ * - ApproximateNumberOfMessagesDelayed: Sum
106
+ * - ApproximateNumberOfMessagesNotVisible: Sum
107
+ */
108
+ function getAggregateData(queueName) {
109
+ return __awaiter(this, void 0, void 0, function () {
110
+ var _a, prefix, suffixRegex, qrls, data, total, _i, data_1, _b, queue, result, key, newAtrribute;
111
+ return __generator(this, function (_c) {
112
+ switch (_c.label) {
113
+ case 0:
114
+ _a = interpretWildcard(queueName), prefix = _a.prefix, suffixRegex = _a.suffixRegex;
115
+ return [4 /*yield*/, (0, sqs_js_1.getMatchingQueues)(prefix, suffixRegex)
116
+ // debug({ qrls })
117
+ ];
118
+ case 1:
119
+ qrls = _c.sent();
120
+ return [4 /*yield*/, (0, sqs_js_1.getQueueAttributes)(qrls)
121
+ // debug({ data })
122
+ ];
123
+ case 2:
124
+ data = _c.sent();
125
+ total = { totalQueues: 0, contributingQueueNames: new Set() };
126
+ for (_i = 0, data_1 = data; _i < data_1.length; _i++) {
127
+ _b = data_1[_i], queue = _b.queue, result = _b.result;
128
+ // debug({ row })
129
+ total.totalQueues++;
130
+ for (key in result.Attributes) {
131
+ newAtrribute = parseInt(result.Attributes[key], 10);
132
+ if (newAtrribute > 0) {
133
+ total.contributingQueueNames.add(queue);
134
+ total[key] = (total[key] || 0) + newAtrribute;
135
+ }
136
+ }
137
+ }
138
+ // debug({ total })
139
+ // convert set to array
140
+ total.contributingQueueNames = __spreadArray([], total.contributingQueueNames.values(), true);
141
+ total.queueName = queueName;
142
+ return [2 /*return*/, total];
143
+ }
144
+ });
145
+ });
146
+ }
147
+ exports.getAggregateData = getAggregateData;
148
+ debug('loaded');
@@ -47,7 +47,6 @@ exports.JobExecutor = void 0;
47
47
  var client_sqs_1 = require("@aws-sdk/client-sqs");
48
48
  var chalk_1 = __importDefault(require("chalk"));
49
49
  var debug_1 = __importDefault(require("debug"));
50
- var sentry_js_1 = require("../sentry.js");
51
50
  var sqs_js_1 = require("../sqs.js");
52
51
  var debug = (0, debug_1.default)('qdone:jobExecutor');
53
52
  var maxJobSeconds = 12 * 60 * 60;
@@ -131,18 +131,38 @@ function getQueueAttributes(qrls) {
131
131
  var command = new client_sqs_1.GetQueueAttributesCommand(input);
132
132
  // debug({ input, command })
133
133
  promises.push((function () { return __awaiter(_this, void 0, void 0, function () {
134
- var queue, result;
134
+ var queue, result, e_1;
135
135
  return __generator(this, function (_a) {
136
136
  switch (_a.label) {
137
137
  case 0:
138
138
  queue = (0, path_1.basename)(qrl);
139
+ _a.label = 1;
140
+ case 1:
141
+ _a.trys.push([1, 3, , 4]);
139
142
  return [4 /*yield*/, client.send(command)
140
143
  // debug({ queue, result })
141
144
  ];
142
- case 1:
145
+ case 2:
143
146
  result = _a.sent();
144
147
  // debug({ queue, result })
145
148
  return [2 /*return*/, { queue: queue, result: result }];
149
+ case 3:
150
+ e_1 = _a.sent();
151
+ if (e_1 instanceof client_sqs_1.QueueDoesNotExist) {
152
+ // For queues that have been deleted in the meantime for whatever
153
+ // reason, just show as having no messages instead of failing the
154
+ // whole batch
155
+ return [2 /*return*/, {
156
+ queue: queue,
157
+ Attributes: {
158
+ ApproximateNumberOfMessages: '0',
159
+ ApproximateNumberOfMessagesNotVisible: '0',
160
+ ApproximateNumberOfMessagesDelayed: '0'
161
+ }
162
+ }];
163
+ }
164
+ throw e_1;
165
+ case 4: return [2 /*return*/];
146
166
  }
147
167
  });
148
168
  }); })());
package/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { enqueue, enqueueBatch } from './src/enqueue.js'
2
2
  export { processMessages, requestShutdown } from './src/consumer.js'
3
+ export { monitor } from './src/monitor.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qdone",
3
- "version": "2.0.19-alpha",
3
+ "version": "2.0.20-alpha",
4
4
  "description": "Language agnostic job queue for SQS",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/monitor.js CHANGED
@@ -3,9 +3,25 @@
3
3
  */
4
4
 
5
5
  import { getMatchingQueues, getQueueAttributes } from './sqs.js'
6
+ import { putAggregateData } from './cloudWatch.js'
7
+ import { getOptionsWithDefaults } from './defaults.js'
6
8
  import Debug from 'debug'
7
9
  const debug = Debug('sd:utils:qmonitor:index')
8
10
 
11
+ /**
12
+ * Splits a queue name with a single wildcard into prefix and suffix regex.
13
+ */
14
+ export async function monitor (queue, save, options) {
15
+ const opt = getOptionsWithDefaults(options)
16
+ const data = await getAggregateData(queue)
17
+ console.log(data)
18
+ if (save) {
19
+ if (opt.verbose) process.stderr.write('Saving to CloudWatch...')
20
+ await putAggregateData(data)
21
+ if (opt.verbose) process.stderr.write('done\n')
22
+ }
23
+ }
24
+
9
25
  /**
10
26
  * Splits a queue name with a single wildcard into prefix and suffix regex.
11
27
  */
@@ -8,7 +8,6 @@ import { ChangeMessageVisibilityBatchCommand, DeleteMessageBatchCommand } from '
8
8
  import chalk from 'chalk'
9
9
  import Debug from 'debug'
10
10
 
11
- import { withSentry } from '../sentry.js'
12
11
  import { getSQSClient } from '../sqs.js'
13
12
 
14
13
  const debug = Debug('qdone:jobExecutor')
package/src/sqs.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Functions that deal with SQS
3
3
  */
4
4
 
5
- import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand } from '@aws-sdk/client-sqs'
5
+ import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand, QueueDoesNotExist } from '@aws-sdk/client-sqs'
6
6
  import { basename } from 'path'
7
7
  import Debug from 'debug'
8
8
  const debug = Debug('qdone:sqs')
@@ -62,9 +62,26 @@ export async function getQueueAttributes (qrls) {
62
62
  // debug({ input, command })
63
63
  promises.push((async () => {
64
64
  const queue = basename(qrl)
65
- const result = await client.send(command)
66
- // debug({ queue, result })
67
- return { queue, result }
65
+ try {
66
+ const result = await client.send(command)
67
+ // debug({ queue, result })
68
+ return { queue, result }
69
+ } catch (e) {
70
+ if (e instanceof QueueDoesNotExist) {
71
+ // For queues that have been deleted in the meantime for whatever
72
+ // reason, just show as having no messages instead of failing the
73
+ // whole batch
74
+ return {
75
+ queue,
76
+ Attributes: {
77
+ ApproximateNumberOfMessages: '0',
78
+ ApproximateNumberOfMessagesNotVisible: '0',
79
+ ApproximateNumberOfMessagesDelayed: '0'
80
+ }
81
+ }
82
+ }
83
+ throw e
84
+ }
68
85
  })())
69
86
  }
70
87
  return Promise.all(promises)
@@ -1,111 +0,0 @@
1
- "use strict";
2
- /**
3
- * Routines for handling Sentry instrumentation
4
- */
5
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
- step((generator = generator.apply(thisArg, _arguments || [])).next());
12
- });
13
- };
14
- var __generator = (this && this.__generator) || function (thisArg, body) {
15
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
16
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
17
- function verb(n) { return function (v) { return step([n, v]); }; }
18
- function step(op) {
19
- if (f) throw new TypeError("Generator is already executing.");
20
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
21
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
22
- if (y = 0, t) op = [op[0] & 2, t.value];
23
- switch (op[0]) {
24
- case 0: case 1: t = op; break;
25
- case 4: _.label++; return { value: op[1], done: false };
26
- case 5: _.label++; y = op[1]; op = [0]; continue;
27
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
28
- default:
29
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
30
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
31
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
32
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
33
- if (t[2]) _.ops.pop();
34
- _.trys.pop(); continue;
35
- }
36
- op = body.call(thisArg, _);
37
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
38
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
39
- }
40
- };
41
- var __importDefault = (this && this.__importDefault) || function (mod) {
42
- return (mod && mod.__esModule) ? mod : { "default": mod };
43
- };
44
- Object.defineProperty(exports, "__esModule", { value: true });
45
- exports.withSentry = void 0;
46
- var debug_1 = __importDefault(require("debug"));
47
- var node_1 = require("@sentry/node");
48
- var debug = (0, debug_1.default)('qdone:sentry');
49
- var sentryWasInit = false;
50
- function withSentry(callback, opt, contexts) {
51
- return __awaiter(this, void 0, void 0, function () {
52
- var result, err_1;
53
- return __generator(this, function (_a) {
54
- switch (_a.label) {
55
- case 0:
56
- debug({ withSentry: { callback: callback, opt: opt, contexts: contexts } });
57
- // Bail if sentry isn't enabled
58
- if (!opt.sentryDsn)
59
- return [2 /*return*/, callback()
60
- // Init sentry if it's not already
61
- ];
62
- // Init sentry if it's not already
63
- if (!sentryWasInit) {
64
- (0, node_1.init)({ dsn: opt.sentryDsn, traceSampleRate: 0 });
65
- sentryWasInit = true;
66
- }
67
- _a.label = 1;
68
- case 1:
69
- _a.trys.push([1, 3, , 5]);
70
- return [4 /*yield*/, callback()];
71
- case 2:
72
- result = _a.sent();
73
- debug({ result: result });
74
- return [2 /*return*/, result];
75
- case 3:
76
- err_1 = _a.sent();
77
- debug({ err: err_1 });
78
- return [4 /*yield*/, (0, node_1.withScope)(function (scope) {
79
- return __awaiter(this, void 0, void 0, function () {
80
- var key, stdout, stderr, sentryResult;
81
- return __generator(this, function (_a) {
82
- switch (_a.label) {
83
- case 0:
84
- scope.setContext('opt', opt);
85
- if (contexts instanceof Object) {
86
- for (key in contexts)
87
- scope.setContext(key, contexts[key]);
88
- if (err_1.stdout && err_1.stderr) {
89
- stdout = err_1.stdout, stderr = err_1.stderr;
90
- scope.setContext('IO', { stdout: stdout, stderr: stderr });
91
- }
92
- }
93
- return [4 /*yield*/, (0, node_1.captureException)(err_1)];
94
- case 1:
95
- sentryResult = _a.sent();
96
- debug({ sentryResult: sentryResult });
97
- return [2 /*return*/];
98
- }
99
- });
100
- });
101
- })];
102
- case 4:
103
- _a.sent();
104
- throw err_1;
105
- case 5: return [2 /*return*/];
106
- }
107
- });
108
- });
109
- }
110
- exports.withSentry = withSentry;
111
- debug('loaded');