@arvo-tools/postgres 1.1.0 → 1.2.1

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.
@@ -0,0 +1,629 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
39
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
44
+ 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;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
64
+ var __rest = (this && this.__rest) || function (s, e) {
65
+ var t = {};
66
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
67
+ t[p] = s[p];
68
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
69
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
70
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
71
+ t[p[i]] = s[p[i]];
72
+ }
73
+ return t;
74
+ };
75
+ var __read = (this && this.__read) || function (o, n) {
76
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
77
+ if (!m) return o;
78
+ var i = m.call(o), r, ar = [], e;
79
+ try {
80
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
81
+ }
82
+ catch (error) { e = { error: error }; }
83
+ finally {
84
+ try {
85
+ if (r && !r.done && (m = i["return"])) m.call(i);
86
+ }
87
+ finally { if (e) throw e.error; }
88
+ }
89
+ return ar;
90
+ };
91
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
92
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
93
+ if (ar || !(i in from)) {
94
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
95
+ ar[i] = from[i];
96
+ }
97
+ }
98
+ return to.concat(ar || Array.prototype.slice.call(from));
99
+ };
100
+ Object.defineProperty(exports, "__esModule", { value: true });
101
+ exports.PostgresEventBroker = void 0;
102
+ var api_1 = require("@opentelemetry/api");
103
+ var arvo_core_1 = require("arvo-core");
104
+ var pg_boss_1 = require("pg-boss");
105
+ var utils_1 = require("./utils");
106
+ /**
107
+ * Queue-based event broker for ArvoEvent handlers with automatic routing,
108
+ * retry logic, and OpenTelemetry tracing support.
109
+ *
110
+ * PostgresEventBroker extends PgBoss to provide event-driven workflow management
111
+ * through persistent PostgreSQL-backed queues. It automatically routes events between
112
+ * registered handlers, ensures reliable delivery with configurable retry policies,
113
+ * and maintains distributed tracing context across the entire workflow.
114
+ *
115
+ * Key capabilities include handler registration with dedicated queues, automatic
116
+ * event routing based on the 'to' field, workflow completion handling, support for
117
+ * domained events, and comprehensive queue statistics for monitoring.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * const broker = new PostgresEventBroker({ connectionString: 'postgres://...' });
122
+ * await broker.start();
123
+ *
124
+ * // Register handlers with retry configuration
125
+ * await broker.register(calculatorHandler, {
126
+ * recreateQueue: true,
127
+ * worker: {
128
+ * concurrency: 5,
129
+ * retryLimit: 3,
130
+ * retryBackoff: true
131
+ * }
132
+ * });
133
+ *
134
+ * // Set up workflow completion handler
135
+ * await broker.onWorkflowComplete({
136
+ * source: 'my.workflow',
137
+ * listener: async (event) => {
138
+ * this.logger.log('Workflow completed:', event.data);
139
+ * }
140
+ * });
141
+ *
142
+ * // Handle domained events (e.g., human approval requests)
143
+ * broker.onDomainedEvent(async (event) => {
144
+ * if (event.domain === 'human.interaction') {
145
+ * await handleHumanApproval(event);
146
+ * }
147
+ * });
148
+ *
149
+ * // Dispatch events using ArvoEventFactory
150
+ * const event = createArvoEventFactory(contract.version('1.0.0')).accepts({
151
+ * source: 'my.workflow',
152
+ * data: { numbers: [1, 2, 3] }
153
+ * });
154
+ * await broker.dispatch(event);
155
+ *
156
+ * // Monitor queue health
157
+ * const stats = await broker.getStats();
158
+ * ```
159
+ */
160
+ var PostgresEventBroker = /** @class */ (function (_super) {
161
+ __extends(PostgresEventBroker, _super);
162
+ function PostgresEventBroker() {
163
+ var _this = _super.apply(this, __spreadArray([], __read(arguments), false)) || this;
164
+ /**
165
+ * Internal registry of handler configurations keyed by handler source.
166
+ */
167
+ _this.handlers = {};
168
+ /**
169
+ * Internal list of all registered queue names.
170
+ */
171
+ _this._queues = [];
172
+ /**
173
+ * Logger instance used for all broker operational logging.
174
+ * Defaults to console but can be replaced via setLogger().
175
+ */
176
+ _this.logger = console;
177
+ /**
178
+ * The configured event source for workflow completion.
179
+ */
180
+ _this.injectionEventSource = null;
181
+ /**
182
+ * Default callback invoked when an event has no registered destination handler.
183
+ */
184
+ _this._onHandlerNotFound = function (event) {
185
+ return _this.logger.error('Handler not found for event', event.toString(2));
186
+ };
187
+ /**
188
+ * Callback invoked when a domained event is encountered during routing.
189
+ */
190
+ _this._onDomainedEvent = function (event) {
191
+ return _this.logger.info('Domained event encountered', event.toString(2));
192
+ };
193
+ return _this;
194
+ }
195
+ Object.defineProperty(PostgresEventBroker.prototype, "queues", {
196
+ /**
197
+ * List of all registered queue names in the broker.
198
+ */
199
+ get: function () {
200
+ return this._queues;
201
+ },
202
+ enumerable: false,
203
+ configurable: true
204
+ });
205
+ /**
206
+ * Sets a custom logger for broker operations.
207
+ *
208
+ * Allows integration with existing logging infrastructure (Winston, Pino, etc.)
209
+ * by providing a logger that implements the ILogger interface.
210
+ *
211
+ * @param logger - Logger instance implementing ILogger interface
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * import winston from 'winston';
216
+ *
217
+ * const logger = winston.createLogger({
218
+ * level: 'info',
219
+ * format: winston.format.json(),
220
+ * transports: [new winston.transports.Console()]
221
+ * });
222
+ *
223
+ * broker.setLogger(logger);
224
+ * ```
225
+ */
226
+ PostgresEventBroker.prototype.setLogger = function (logger) {
227
+ this.logger = logger;
228
+ };
229
+ /**
230
+ * Registers a handler for workflow completion events.
231
+ *
232
+ * This sets up the terminal point where events return after flowing through
233
+ * the handler chain. The handler receives events whose 'to' field matches
234
+ * the configured source, typically indicating workflow completion.
235
+ *
236
+ * Sets the injection event source that must match the source of events
237
+ * dispatched via the dispatch() method.
238
+ *
239
+ * **Note:** The listener must handle its own errors. Exceptions are caught
240
+ * and logged but do not cause job failures. This is by design.
241
+ *
242
+ * @param param - Configuration object
243
+ * @param param.source - Event source identifier for completion events
244
+ * @param param.listener - Callback invoked when completion events are received
245
+ * @param param.options - Optional queue and worker configuration
246
+ *
247
+ * @example
248
+ * ```typescript
249
+ * await broker.onWorkflowComplete({
250
+ * source: 'test.test.test',
251
+ * listener: async (event) => {
252
+ * try {
253
+ * this.logger.log('Final result:', event.data);
254
+ * } catch (error) {
255
+ * logger.error('Completion handler failed', error);
256
+ * }
257
+ * },
258
+ * });
259
+ * ```
260
+ */
261
+ PostgresEventBroker.prototype.onWorkflowComplete = function (param) {
262
+ return __awaiter(this, void 0, void 0, function () {
263
+ var i;
264
+ var _this = this;
265
+ var _a, _b, _c, _d, _e, _f, _g, _h;
266
+ return __generator(this, function (_j) {
267
+ switch (_j.label) {
268
+ case 0:
269
+ this.injectionEventSource = param.source;
270
+ this.handlers[param.source] = { options: param.options };
271
+ if (!((_a = param.options) === null || _a === void 0 ? void 0 : _a.recreateQueue)) return [3 /*break*/, 2];
272
+ return [4 /*yield*/, this.deleteQueue(param.source)];
273
+ case 1:
274
+ _j.sent();
275
+ _j.label = 2;
276
+ case 2: return [4 /*yield*/, this.createQueue(param.source, (_b = param.options) === null || _b === void 0 ? void 0 : _b.queue)];
277
+ case 3:
278
+ _j.sent();
279
+ i = 0;
280
+ _j.label = 4;
281
+ case 4:
282
+ if (!(i < Math.max((_e = (_d = (_c = param.options) === null || _c === void 0 ? void 0 : _c.worker) === null || _d === void 0 ? void 0 : _d.concurrency) !== null && _e !== void 0 ? _e : 0, 1))) return [3 /*break*/, 7];
283
+ return [4 /*yield*/, this.work(param.source, {
284
+ pollingIntervalSeconds: (_h = (_g = (_f = param.options) === null || _f === void 0 ? void 0 : _f.worker) === null || _g === void 0 ? void 0 : _g.pollingIntervalSeconds) !== null && _h !== void 0 ? _h : 2,
285
+ batchSize: 1,
286
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
287
+ var eventFromJob_1, error_1;
288
+ var _this = this;
289
+ var _c = __read(_b, 1), job = _c[0];
290
+ return __generator(this, function (_d) {
291
+ switch (_d.label) {
292
+ case 0:
293
+ _d.trys.push([0, 2, , 3]);
294
+ eventFromJob_1 = (0, utils_1.createArvoEventFromJob)(job);
295
+ return [4 /*yield*/, api_1.context.with((0, utils_1.otelParentContext)(eventFromJob_1), function () { return __awaiter(_this, void 0, void 0, function () {
296
+ return __generator(this, function (_a) {
297
+ switch (_a.label) {
298
+ case 0: return [4 /*yield*/, param.listener(eventFromJob_1)];
299
+ case 1: return [2 /*return*/, _a.sent()];
300
+ }
301
+ });
302
+ }); })];
303
+ case 1:
304
+ _d.sent();
305
+ return [3 /*break*/, 3];
306
+ case 2:
307
+ error_1 = _d.sent();
308
+ this.logger.error("[onWorkflowComplete] Error in worker handler for ".concat(param.source), error_1);
309
+ return [3 /*break*/, 3];
310
+ case 3: return [2 /*return*/];
311
+ }
312
+ });
313
+ }); })];
314
+ case 5:
315
+ _j.sent();
316
+ _j.label = 6;
317
+ case 6:
318
+ i++;
319
+ return [3 /*break*/, 4];
320
+ case 7: return [2 /*return*/];
321
+ }
322
+ });
323
+ });
324
+ };
325
+ /**
326
+ * Sets a custom handler for events with no registered destination.
327
+ *
328
+ * When a handler emits an event whose 'to' field doesn't match any
329
+ * registered handler, this callback is invoked. Useful for logging
330
+ * routing errors or implementing fallback behavior.
331
+ *
332
+ * **Note:** The listener must handle its own errors. Exceptions are
333
+ * suppressed by design to prevent routing failures from cascading.
334
+ *
335
+ * @param listner - Callback invoked with unroutable events
336
+ *
337
+ * @example
338
+ * ```typescript
339
+ * broker.onHandlerNotFound(async (event) => {
340
+ * try {
341
+ * logger.error('No handler for', event.to);
342
+ * } catch (error) {
343
+ * this.logger.error('Failed to log missing handler', error);
344
+ * }
345
+ * });
346
+ * ```
347
+ */
348
+ PostgresEventBroker.prototype.onHandlerNotFound = function (listner) {
349
+ this._onHandlerNotFound = listner;
350
+ };
351
+ /**
352
+ * Sets a custom handler for domained events.
353
+ *
354
+ * Domained events are intercepted during routing and passed to this handler
355
+ * instead of being sent to a queue. Useful for handling external system
356
+ * interactions like human approvals or notifications.
357
+ *
358
+ * **Note:** The listener must handle its own errors. Exceptions are
359
+ * suppressed by design to prevent domained event failures from breaking workflows.
360
+ *
361
+ * @param listner - Callback invoked when domained events are encountered
362
+ *
363
+ * @example
364
+ * ```typescript
365
+ * broker.onDomainedEvent(async (event) => {
366
+ * try {
367
+ * if (event.domain === 'notification') {
368
+ * await alerting.notify(event);
369
+ * }
370
+ * } catch (error) {
371
+ * logger.error('Domained event handler failed', error);
372
+ * }
373
+ * });
374
+ * ```
375
+ */
376
+ PostgresEventBroker.prototype.onDomainedEvent = function (listner) {
377
+ this._onDomainedEvent = listner;
378
+ };
379
+ /**
380
+ * Creates a queue and tracks it in the internal queue registry.
381
+ * Overrides the base class method to maintain queue tracking.
382
+ *
383
+ * @param name - Queue name
384
+ * @param options - Queue configuration options
385
+ */
386
+ PostgresEventBroker.prototype.createQueue = function (name, options) {
387
+ return __awaiter(this, void 0, void 0, function () {
388
+ return __generator(this, function (_a) {
389
+ this._queues.push(name);
390
+ return [2 /*return*/, _super.prototype.createQueue.call(this, name, options)];
391
+ });
392
+ });
393
+ };
394
+ /**
395
+ * Registers an event handler with the broker.
396
+ *
397
+ * Creates a dedicated queue for the handler and spawns worker instances
398
+ * to process incoming events. Events emitted by the handler are automatically
399
+ * routed to their destinations based on the 'to' field.
400
+ *
401
+ * @param handler - The ArvoEvent handler to register
402
+ * @param options - Configuration for queue behavior, worker concurrency, and retry policies
403
+ * @throws {Error} If a handler with the same source is already registered
404
+ *
405
+ * @example
406
+ * ```typescript
407
+ * await broker.register(calculatorHandler, {
408
+ * recreateQueue: true,
409
+ * queue: { deadLetter: 'dlq' },
410
+ * worker: {
411
+ * concurrency: 5,
412
+ * onError: async (job, error) => {
413
+ * if (error.message.includes('timeout')) return 'RETRY';
414
+ * return 'FAIL';
415
+ * }
416
+ * }
417
+ * });
418
+ * ```
419
+ */
420
+ PostgresEventBroker.prototype.register = function (handler, options) {
421
+ return __awaiter(this, void 0, void 0, function () {
422
+ var handlerSource, workHandler, i;
423
+ var _this = this;
424
+ var _a, _b, _c, _d;
425
+ return __generator(this, function (_e) {
426
+ switch (_e.label) {
427
+ case 0:
428
+ if (this.handlers[handler.source]) {
429
+ throw new Error((0, arvo_core_1.cleanString)("\n Handler registration failed: A handler with source '".concat(handler.source, "' is already registered.\n Each handler must have a unique source identifier. Attempted duplicate registration will be\n ignored to prevent queue conflicts.\n ")));
430
+ }
431
+ this.handlers[handler.source] = { options: __assign({}, options) };
432
+ if (!(options === null || options === void 0 ? void 0 : options.recreateQueue)) return [3 /*break*/, 2];
433
+ return [4 /*yield*/, this.deleteQueue(handler.source)];
434
+ case 1:
435
+ _e.sent();
436
+ _e.label = 2;
437
+ case 2: return [4 /*yield*/, this.createQueue(handler.source, options === null || options === void 0 ? void 0 : options.queue)];
438
+ case 3:
439
+ _e.sent();
440
+ handlerSource = handler.source;
441
+ workHandler = function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
442
+ var eventFromJob_2, events, error_2;
443
+ var _this = this;
444
+ var _c = __read(_b, 1), job = _c[0];
445
+ return __generator(this, function (_d) {
446
+ switch (_d.label) {
447
+ case 0:
448
+ _d.trys.push([0, 3, , 4]);
449
+ eventFromJob_2 = (0, utils_1.createArvoEventFromJob)(job);
450
+ return [4 /*yield*/, api_1.context.with((0, utils_1.otelParentContext)(eventFromJob_2), function () { return __awaiter(_this, void 0, void 0, function () {
451
+ return __generator(this, function (_a) {
452
+ switch (_a.label) {
453
+ case 0: return [4 /*yield*/, handler.execute(eventFromJob_2, {
454
+ inheritFrom: 'EVENT',
455
+ })];
456
+ case 1: return [2 /*return*/, _a.sent()];
457
+ }
458
+ });
459
+ }); })];
460
+ case 1:
461
+ events = (_d.sent()).events;
462
+ return [4 /*yield*/, Promise.all(events.map(function (evt) { return __awaiter(_this, void 0, void 0, function () {
463
+ var error_3;
464
+ var _a;
465
+ return __generator(this, function (_b) {
466
+ switch (_b.label) {
467
+ case 0:
468
+ if (!evt.domain) return [3 /*break*/, 5];
469
+ _b.label = 1;
470
+ case 1:
471
+ _b.trys.push([1, 3, , 4]);
472
+ return [4 /*yield*/, ((_a = this._onDomainedEvent) === null || _a === void 0 ? void 0 : _a.call(this, evt))];
473
+ case 2:
474
+ _b.sent();
475
+ return [3 /*break*/, 4];
476
+ case 3:
477
+ error_3 = _b.sent();
478
+ this.logger.error('Error in onDomainedEvent', error_3);
479
+ return [3 /*break*/, 4];
480
+ case 4: return [2 /*return*/, undefined];
481
+ case 5: return [4 /*yield*/, this._emitArvoEvent(evt)];
482
+ case 6: return [2 /*return*/, _b.sent()];
483
+ }
484
+ });
485
+ }); }))];
486
+ case 2:
487
+ _d.sent();
488
+ return [3 /*break*/, 4];
489
+ case 3:
490
+ error_2 = _d.sent();
491
+ this.logger.error("Error in worker handler for ".concat(handlerSource), error_2);
492
+ throw error_2;
493
+ case 4: return [2 /*return*/];
494
+ }
495
+ });
496
+ }); };
497
+ i = 0;
498
+ _e.label = 4;
499
+ case 4:
500
+ if (!(i < Math.max((_b = (_a = options === null || options === void 0 ? void 0 : options.worker) === null || _a === void 0 ? void 0 : _a.concurrency) !== null && _b !== void 0 ? _b : 0, 1))) return [3 /*break*/, 7];
501
+ return [4 /*yield*/, this.work(handler.source, {
502
+ pollingIntervalSeconds: (_d = (_c = options === null || options === void 0 ? void 0 : options.worker) === null || _c === void 0 ? void 0 : _c.pollingIntervalSeconds) !== null && _d !== void 0 ? _d : 2,
503
+ batchSize: 1,
504
+ }, workHandler)];
505
+ case 5:
506
+ _e.sent();
507
+ _e.label = 6;
508
+ case 6:
509
+ i++;
510
+ return [3 /*break*/, 4];
511
+ case 7: return [2 /*return*/];
512
+ }
513
+ });
514
+ });
515
+ };
516
+ /**
517
+ * Routes an ArvoEvent to its destination queue.
518
+ *
519
+ * Internal method that filters out worker configuration options and includes
520
+ * only job-level options when sending the event. This method performs no
521
+ * validation and assumes the event and handler have already been verified.
522
+ *
523
+ * @param event - The ArvoEvent to route
524
+ * @returns Job ID if sent successfully, null if handler not found
525
+ */
526
+ PostgresEventBroker.prototype._emitArvoEvent = function (event) {
527
+ return __awaiter(this, void 0, void 0, function () {
528
+ var e_1, _a, concurrency, pollingIntervalSeconds, rest;
529
+ var _b, _c, _d;
530
+ return __generator(this, function (_e) {
531
+ switch (_e.label) {
532
+ case 0:
533
+ if (!(!event.to || !this._queues.includes(event.to))) return [3 /*break*/, 5];
534
+ _e.label = 1;
535
+ case 1:
536
+ _e.trys.push([1, 3, , 4]);
537
+ return [4 /*yield*/, ((_b = this._onHandlerNotFound) === null || _b === void 0 ? void 0 : _b.call(this, event))];
538
+ case 2:
539
+ _e.sent();
540
+ return [3 /*break*/, 4];
541
+ case 3:
542
+ e_1 = _e.sent();
543
+ this.logger.error('Error in onHandlerNotFound ', e_1);
544
+ return [3 /*break*/, 4];
545
+ case 4: return [2 /*return*/, null];
546
+ case 5:
547
+ _a = (_d = (_c = this.handlers[event.to].options) === null || _c === void 0 ? void 0 : _c.worker) !== null && _d !== void 0 ? _d : {}, concurrency = _a.concurrency, pollingIntervalSeconds = _a.pollingIntervalSeconds, rest = __rest(_a, ["concurrency", "pollingIntervalSeconds"]);
548
+ return [4 /*yield*/, this.send(event.to, event.toJSON(), __assign({}, (rest !== null && rest !== void 0 ? rest : {})))];
549
+ case 6: return [2 /*return*/, _e.sent()];
550
+ }
551
+ });
552
+ });
553
+ };
554
+ /**
555
+ * Dispatches an ArvoEvent into the broker system with validation.
556
+ *
557
+ * This is the primary entry point for injecting events into the workflow.
558
+ * The event must originate from the source configured in onWorkflowComplete()
559
+ * and target a registered handler.
560
+ *
561
+ * @param event - The ArvoEvent to dispatch
562
+ * @returns Job ID assigned by the queue system
563
+ * @throws {Error} If workflow completion handler is not configured
564
+ * @throws {Error} If event source doesn't match configured workflow source
565
+ * @throws {Error} If target handler is not registered
566
+ *
567
+ * @example
568
+ * ```typescript
569
+ * const event = createArvoEvent({
570
+ * source: 'my.workflow',
571
+ * to: 'calculator.handler',
572
+ * type: 'calculate.request',
573
+ * data: { operation: 'add', values: [1, 2] }
574
+ * });
575
+ *
576
+ * await broker.dispatch(event);
577
+ * ```
578
+ */
579
+ PostgresEventBroker.prototype.dispatch = function (event) {
580
+ return __awaiter(this, void 0, void 0, function () {
581
+ var _a;
582
+ return __generator(this, function (_b) {
583
+ switch (_b.label) {
584
+ case 0:
585
+ if (!this.injectionEventSource) {
586
+ throw new Error((0, arvo_core_1.cleanString)("\n Workflow completion handler not configured: Cannot dispatch ArvoEvent without setting up\n the workflow completion handler. Call onWorkflowComplete({ source: string, handler: Function })\n to register the completion point before dispatching events into the system.\n "));
587
+ }
588
+ if (this.injectionEventSource !== event.source) {
589
+ throw new Error((0, arvo_core_1.cleanString)("\n Event source mismatch: The dispatched event source '".concat(event.source, "' does not match the\n configured workflow completion source '").concat(this.injectionEventSource, "'. Events dispatched\n through dispatch() must originate from the source specified in onWorkflowComplete().\n Verify the event's source property matches '").concat(this.injectionEventSource, "'.\n ")));
590
+ }
591
+ if (!this._queues.includes((_a = event.to) !== null && _a !== void 0 ? _a : '')) {
592
+ throw new Error((0, arvo_core_1.cleanString)("\n Handler not registered: No handler found for destination '".concat(event.to, "'. The target handler\n must be registered using register() before events can be routed to it. Register the required\n handler or verify the event's 'to' property is correct.\n ")));
593
+ }
594
+ return [4 /*yield*/, this._emitArvoEvent(event)];
595
+ case 1: return [2 /*return*/, _b.sent()];
596
+ }
597
+ });
598
+ });
599
+ };
600
+ /**
601
+ * Retrieves statistics for all registered queues in the broker.
602
+ *
603
+ * @returns Array of queue statistics including active, queued, and total job counts
604
+ *
605
+ * @example
606
+ * ```typescript
607
+ * const stats = await broker.getStats();
608
+ * stats.forEach(stat => {
609
+ * this.logger.log(`Queue ${stat.name}: ${stat.activeCount} active, ${stat.queuedCount} queued`);
610
+ * });
611
+ * ```
612
+ */
613
+ PostgresEventBroker.prototype.getStats = function () {
614
+ return __awaiter(this, void 0, void 0, function () {
615
+ var _this = this;
616
+ return __generator(this, function (_a) {
617
+ switch (_a.label) {
618
+ case 0: return [4 /*yield*/, Promise.all(this._queues.map(function (q) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
619
+ return [2 /*return*/, this.getQueueStats(q)];
620
+ }); }); }))];
621
+ case 1: return [2 /*return*/, _a.sent()];
622
+ }
623
+ });
624
+ });
625
+ };
626
+ return PostgresEventBroker;
627
+ }(pg_boss_1.PgBoss));
628
+ exports.PostgresEventBroker = PostgresEventBroker;
629
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/broker/index.ts"],"names":[],"mappings":";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