@diia-inhouse/diia-queue 7.3.0 → 13.2.0
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/LICENCE.md +59 -59
- package/dist/constants.js +3 -0
- package/dist/constants.js.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/interfaces/index.js +12 -9
- package/dist/interfaces/index.js.map +1 -1
- package/dist/interfaces/messageBrokerServiceConfig.js +26 -0
- package/dist/interfaces/messageBrokerServiceConfig.js.map +1 -0
- package/dist/interfaces/messageHandler.js +3 -0
- package/dist/interfaces/messageHandler.js.map +1 -0
- package/dist/interfaces/metrics/index.js +14 -0
- package/dist/interfaces/metrics/index.js.map +1 -1
- package/dist/interfaces/{providers/rabbitmq/amqpListener.js → options.js} +1 -1
- package/dist/interfaces/options.js.map +1 -0
- package/dist/interfaces/providers/rabbitmq/amqpConnection.js +4 -1
- package/dist/interfaces/providers/rabbitmq/amqpConnection.js.map +1 -1
- package/dist/interfaces/providers/rabbitmq/amqpPublisher.js +1 -9
- package/dist/interfaces/providers/rabbitmq/amqpPublisher.js.map +1 -1
- package/dist/interfaces/providers/rabbitmq/index.js +41 -17
- package/dist/interfaces/providers/rabbitmq/index.js.map +1 -1
- package/dist/interfaces/queueContext.js +3 -0
- package/dist/interfaces/queueContext.js.map +1 -0
- package/dist/interfaces/queueStatus.js +6 -0
- package/dist/interfaces/queueStatus.js.map +1 -1
- package/dist/interfaces/services/eventMessageHandler.js +3 -0
- package/dist/interfaces/services/eventMessageHandler.js.map +1 -0
- package/dist/metrics/index.js +1 -2
- package/dist/metrics/index.js.map +1 -1
- package/dist/providers/index.js +22 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/rabbitmq/amqpAsserter.js +417 -0
- package/dist/providers/rabbitmq/amqpAsserter.js.map +1 -0
- package/dist/providers/rabbitmq/amqpConnection.js +47 -25
- package/dist/providers/rabbitmq/amqpConnection.js.map +1 -1
- package/dist/providers/rabbitmq/amqpListener.js +167 -113
- package/dist/providers/rabbitmq/amqpListener.js.map +1 -1
- package/dist/providers/rabbitmq/amqpPublisher.js +147 -96
- package/dist/providers/rabbitmq/amqpPublisher.js.map +1 -1
- package/dist/providers/rabbitmq/index.js +106 -286
- package/dist/providers/rabbitmq/index.js.map +1 -1
- package/dist/services/communicator.js +187 -0
- package/dist/services/communicator.js.map +1 -0
- package/dist/services/eventBus.js +41 -46
- package/dist/services/eventBus.js.map +1 -1
- package/dist/services/eventCommunicator.js +125 -0
- package/dist/services/eventCommunicator.js.map +1 -0
- package/dist/services/eventMessageHandler.js +75 -91
- package/dist/services/eventMessageHandler.js.map +1 -1
- package/dist/services/eventMessageValidator.js.map +1 -1
- package/dist/services/externalCommunicator.js +11 -104
- package/dist/services/externalCommunicator.js.map +1 -1
- package/dist/services/externalEventBus.js +147 -55
- package/dist/services/externalEventBus.js.map +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/index.js.map +1 -1
- package/dist/services/metrics.js +49 -0
- package/dist/services/metrics.js.map +1 -0
- package/dist/services/optionsBuilder.js +59 -0
- package/dist/services/optionsBuilder.js.map +1 -0
- package/dist/services/queue.js +63 -26
- package/dist/services/queue.js.map +1 -1
- package/dist/services/scheduledTask.js +52 -48
- package/dist/services/scheduledTask.js.map +1 -1
- package/dist/services/task.js +149 -28
- package/dist/services/task.js.map +1 -1
- package/dist/types/constants.d.ts +3 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/interfaces/deps.d.ts +11 -2
- package/dist/types/interfaces/externalCommunicator.d.ts +5 -12
- package/dist/types/interfaces/index.d.ts +70 -60
- package/dist/types/interfaces/messageBrokerServiceConfig.d.ts +79 -0
- package/dist/types/interfaces/messageHandler.d.ts +2 -0
- package/dist/types/interfaces/metrics/index.d.ts +13 -0
- package/dist/types/interfaces/options.d.ts +50 -0
- package/dist/types/interfaces/providers/rabbitmq/amqpConnection.d.ts +3 -0
- package/dist/types/interfaces/providers/rabbitmq/amqpPublisher.d.ts +9 -17
- package/dist/types/interfaces/providers/rabbitmq/index.d.ts +72 -8
- package/dist/types/interfaces/queueConfig/configs.d.ts +19 -11
- package/dist/types/interfaces/queueContext.d.ts +4 -0
- package/dist/types/interfaces/queueStatus.d.ts +7 -2
- package/dist/types/interfaces/services/eventMessageHandler.d.ts +5 -0
- package/dist/types/metrics/index.d.ts +1 -2
- package/dist/types/providers/index.d.ts +5 -0
- package/dist/types/providers/rabbitmq/amqpAsserter.d.ts +49 -0
- package/dist/types/providers/rabbitmq/amqpConnection.d.ts +3 -7
- package/dist/types/providers/rabbitmq/amqpListener.d.ts +27 -11
- package/dist/types/providers/rabbitmq/amqpPublisher.d.ts +26 -17
- package/dist/types/providers/rabbitmq/index.d.ts +35 -38
- package/dist/types/services/communicator.d.ts +57 -0
- package/dist/types/services/eventBus.d.ts +16 -12
- package/dist/types/services/eventCommunicator.d.ts +15 -0
- package/dist/types/services/eventMessageHandler.d.ts +9 -11
- package/dist/types/services/eventMessageValidator.d.ts +1 -1
- package/dist/types/services/externalCommunicator.d.ts +6 -19
- package/dist/types/services/externalEventBus.d.ts +28 -13
- package/dist/types/services/index.d.ts +1 -1
- package/dist/types/services/metrics.d.ts +11 -0
- package/dist/types/services/optionsBuilder.d.ts +10 -0
- package/dist/types/services/queue.d.ts +14 -9
- package/dist/types/services/scheduledTask.d.ts +22 -14
- package/dist/types/services/task.d.ts +21 -11
- package/dist/types/utils.d.ts +2 -2
- package/dist/utils.js +5 -2
- package/dist/utils.js.map +1 -1
- package/package.json +38 -47
- package/dist/interfaces/providers/rabbitmq/amqpListener.js.map +0 -1
- package/dist/services/externalCommunicatorChannel.js +0 -31
- package/dist/services/externalCommunicatorChannel.js.map +0 -1
- package/dist/types/interfaces/providers/rabbitmq/amqpListener.d.ts +0 -7
- package/dist/types/services/externalCommunicatorChannel.d.ts +0 -11
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventMessageHandler = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
4
5
|
const api_1 = require("@opentelemetry/api");
|
|
5
|
-
const uuid_1 = require("uuid");
|
|
6
6
|
const errors_1 = require("@diia-inhouse/errors");
|
|
7
7
|
const utils_1 = require("@diia-inhouse/utils");
|
|
8
|
-
const
|
|
8
|
+
const interfaces_1 = require("../interfaces");
|
|
9
9
|
class EventMessageHandler {
|
|
10
10
|
eventMessageValidator;
|
|
11
|
-
externalChannel;
|
|
12
|
-
pubsub;
|
|
13
11
|
asyncLocalStorage;
|
|
14
12
|
logger;
|
|
15
|
-
|
|
13
|
+
noRequeueNackOptions = new interfaces_1.NackOptions(false, false);
|
|
14
|
+
constructor(eventMessageValidator, asyncLocalStorage, logger) {
|
|
16
15
|
this.eventMessageValidator = eventMessageValidator;
|
|
17
|
-
this.externalChannel = externalChannel;
|
|
18
|
-
this.pubsub = pubsub;
|
|
19
16
|
this.asyncLocalStorage = asyncLocalStorage;
|
|
20
17
|
this.logger = logger;
|
|
21
18
|
}
|
|
@@ -27,110 +24,90 @@ class EventMessageHandler {
|
|
|
27
24
|
await this.eventListenerMessageHandler(listener, message);
|
|
28
25
|
}
|
|
29
26
|
async eventListenerMessageHandler(listener, message) {
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const activeSpanTraceId = api_1.trace.getActiveSpan()?.spanContext().traceId;
|
|
34
|
-
const traceId = (0, api_1.isValidTraceId)(activeSpanTraceId) ? activeSpanTraceId : properties.headers?.traceId || (0, uuid_1.v4)();
|
|
35
|
-
const context = {
|
|
36
|
-
logData: {
|
|
37
|
-
traceId,
|
|
38
|
-
serviceCode: this.getServiceCode(listener, payload),
|
|
39
|
-
},
|
|
40
|
-
};
|
|
27
|
+
const { done, properties, data: { event, payload }, } = message;
|
|
28
|
+
const serviceCode = this.getServiceCode(listener, payload);
|
|
29
|
+
const context = this.prepareAsyncContext(properties, serviceCode);
|
|
41
30
|
await this.asyncLocalStorage.run(context, async () => {
|
|
42
|
-
this.logger.
|
|
31
|
+
this.logger.info(`Handling event [${event}] with payload`, { payload });
|
|
43
32
|
if (!listener) {
|
|
44
|
-
this.logger.info(`
|
|
33
|
+
this.logger.info(`Not found listener for the event [${event}]`);
|
|
45
34
|
return done();
|
|
46
35
|
}
|
|
47
|
-
|
|
48
|
-
return await this.syncMessageHandler(listener, message, payloadValidationSchema);
|
|
49
|
-
}
|
|
50
|
-
try {
|
|
51
|
-
this.eventMessageValidator.validateEventMessage(data, payloadValidationSchema);
|
|
52
|
-
const resp = await listener.handler?.(payload, meta);
|
|
53
|
-
return this.performDone(message, resp, false);
|
|
54
|
-
}
|
|
55
|
-
catch (err) {
|
|
56
|
-
if (err instanceof errors_1.ValidationError && 'validationErrorHandler' in listener && payload?.uuid) {
|
|
57
|
-
await listener.validationErrorHandler?.(err, payload.uuid).catch((err_) => err_);
|
|
58
|
-
}
|
|
59
|
-
this.logger.error(`Failed to handle event ${event}`, { err });
|
|
60
|
-
metrics_1.totalMessageHandlerErrorsMetrics.increment({ event });
|
|
61
|
-
// TODO(BACK-0): message.reject();
|
|
62
|
-
return this.performDone(message, err, true);
|
|
63
|
-
}
|
|
36
|
+
return await this.handleMessage(listener, message);
|
|
64
37
|
});
|
|
65
38
|
}
|
|
66
|
-
|
|
67
|
-
const
|
|
39
|
+
prepareAsyncContext(properties, serviceCode) {
|
|
40
|
+
const activeSpanTraceId = api_1.trace.getActiveSpan()?.spanContext().traceId ?? '';
|
|
41
|
+
const traceId = (0, api_1.isValidTraceId)(activeSpanTraceId) ? activeSpanTraceId : properties.headers?.traceId || (0, node_crypto_1.randomUUID)();
|
|
42
|
+
return {
|
|
43
|
+
logData: {
|
|
44
|
+
traceId,
|
|
45
|
+
serviceCode,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async handleMessage(listener, message) {
|
|
50
|
+
const { data, done, reject, properties: { replyTo, correlationId }, } = message;
|
|
68
51
|
const { event, payload, meta } = data;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
this.eventMessageValidator.validateSyncedEventMessage(data, payloadValidationSchema);
|
|
78
|
-
}
|
|
79
|
-
catch (err) {
|
|
80
|
-
this.logger.error(`Message in a wrong format was received from a synced event: ${event}`, { err });
|
|
81
|
-
return done();
|
|
82
|
-
}
|
|
83
|
-
if (payload.error) {
|
|
84
|
-
this.logger.error(`Error received for a synced event: ${event}`, payload.error);
|
|
85
|
-
return done();
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
await listener.handler(payload, meta);
|
|
89
|
-
}
|
|
90
|
-
catch (err) {
|
|
91
|
-
this.logger.error(`Failed to handle the synced event [${event}] with payload`, { err });
|
|
52
|
+
const useDirectReply = replyTo && correlationId;
|
|
53
|
+
let hasErrorOccurred = false;
|
|
54
|
+
let result;
|
|
55
|
+
const { isValid, error } = await this.validateData(data, listener);
|
|
56
|
+
if (!isValid) {
|
|
57
|
+
if (useDirectReply && error) {
|
|
58
|
+
return this.directReplyDone(message, error, true);
|
|
92
59
|
}
|
|
60
|
+
return reject(this.noRequeueNackOptions);
|
|
93
61
|
}
|
|
94
|
-
|
|
95
|
-
await
|
|
62
|
+
try {
|
|
63
|
+
result = await listener.handler?.(payload, meta);
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
result = err;
|
|
67
|
+
hasErrorOccurred = true;
|
|
68
|
+
this.logger.error(`Failed to handle event ${event}`, { err });
|
|
69
|
+
}
|
|
70
|
+
if (useDirectReply) {
|
|
71
|
+
return this.directReplyDone(message, result, hasErrorOccurred);
|
|
72
|
+
}
|
|
73
|
+
if (result instanceof interfaces_1.NackOptions) {
|
|
74
|
+
return reject(result);
|
|
75
|
+
}
|
|
76
|
+
else if (hasErrorOccurred && listener.nackOptions) {
|
|
77
|
+
return reject(listener.nackOptions);
|
|
96
78
|
}
|
|
97
79
|
return done();
|
|
98
80
|
}
|
|
99
|
-
|
|
100
|
-
const {
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return;
|
|
81
|
+
async validateData(data, listener) {
|
|
82
|
+
const { payload } = data;
|
|
83
|
+
const { validationRules } = listener;
|
|
84
|
+
try {
|
|
85
|
+
this.eventMessageValidator.validateEventMessage(data, validationRules);
|
|
86
|
+
return { isValid: true };
|
|
105
87
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
payload: {
|
|
114
|
-
uuid: payload.uuid,
|
|
115
|
-
error: {
|
|
116
|
-
message: err.message,
|
|
117
|
-
http_code: err.getCode(),
|
|
118
|
-
data: err.getData(),
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
});
|
|
88
|
+
catch (err) {
|
|
89
|
+
this.logger.error('Failed to validate event message', { err });
|
|
90
|
+
if (err instanceof errors_1.ValidationError && 'validationErrorHandler' in listener && payload?.uuid) {
|
|
91
|
+
const eventBusListener = listener;
|
|
92
|
+
await eventBusListener.validationErrorHandler?.(err, payload.uuid).catch((err_) => err_);
|
|
93
|
+
}
|
|
94
|
+
return err instanceof errors_1.ValidationError ? { isValid: false, error: err } : { isValid: false };
|
|
123
95
|
}
|
|
124
|
-
|
|
96
|
+
}
|
|
97
|
+
directReplyDone(receivedMessage, response, error) {
|
|
98
|
+
const { done, data: { event, payload: { uuid }, }, } = receivedMessage;
|
|
99
|
+
const data = error ? utils_1.utils.handleError(response, (err) => err) : response;
|
|
100
|
+
const messageData = {
|
|
125
101
|
event,
|
|
126
102
|
meta: {
|
|
127
103
|
date: new Date(),
|
|
128
104
|
},
|
|
129
105
|
payload: {
|
|
130
|
-
uuid
|
|
131
|
-
response: data,
|
|
106
|
+
uuid,
|
|
107
|
+
...(data instanceof errors_1.ApiError ? { error: this.prepareQueueMessageError(data) } : { response: data }),
|
|
132
108
|
},
|
|
133
|
-
}
|
|
109
|
+
};
|
|
110
|
+
done(messageData);
|
|
134
111
|
}
|
|
135
112
|
getServiceCode(listener, payload) {
|
|
136
113
|
try {
|
|
@@ -140,6 +117,13 @@ class EventMessageHandler {
|
|
|
140
117
|
this.logger.error('Failed to get event listener service code', { err, listener });
|
|
141
118
|
}
|
|
142
119
|
}
|
|
120
|
+
prepareQueueMessageError(err) {
|
|
121
|
+
return {
|
|
122
|
+
data: err.getData(),
|
|
123
|
+
message: err.message,
|
|
124
|
+
http_code: err.getCode(),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
143
127
|
}
|
|
144
128
|
exports.EventMessageHandler = EventMessageHandler;
|
|
145
129
|
//# sourceMappingURL=eventMessageHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventMessageHandler.js","sourceRoot":"","sources":["../../src/services/eventMessageHandler.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"eventMessageHandler.js","sourceRoot":"","sources":["../../src/services/eventMessageHandler.ts"],"names":[],"mappings":";;;AACA,6CAAwC;AAExC,4CAA0D;AAG1D,iDAAgE;AAEhE,+CAA2C;AAE3C,8CAWsB;AAKtB,MAAa,mBAAmB;IAIP;IACA;IACA;IALJ,oBAAoB,GAAG,IAAI,wBAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IAErE,YACqB,qBAA4C,EAC5C,iBAAkD,EAClD,MAAc;QAFd,0BAAqB,GAArB,qBAAqB,CAAuB;QAC5C,sBAAiB,GAAjB,iBAAiB,CAAiC;QAClD,WAAM,GAAN,MAAM,CAAQ;IAChC,CAAC;IAEJ,KAAK,CAAC,4BAA4B,CAAa,cAA8B,EAAE,OAA4B;QACvG,IAAI,CAAC,OAAO,EAAE,CAAC;YACX,OAAM;QACV,CAAC;QAED,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,KA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AC/C,CAAC;CACJ;AAxCD,sDAwCC"}
|
|
@@ -1,134 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.ExternalCommunicator = void 0;
|
|
27
|
-
const
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
28
5
|
const errors_1 = require("@diia-inhouse/errors");
|
|
29
|
-
const Utils = __importStar(require("../utils"));
|
|
30
6
|
class ExternalCommunicator {
|
|
31
|
-
externalChannel;
|
|
32
7
|
externalEventBus;
|
|
33
|
-
externalEventListenerList;
|
|
34
8
|
eventMessageValidator;
|
|
35
9
|
logger;
|
|
36
|
-
|
|
37
|
-
timeout;
|
|
38
|
-
eventListeners;
|
|
39
|
-
constructor(externalChannel, externalEventBus, externalEventListenerList, eventMessageValidator, logger, pubsub, timeout = 10000) {
|
|
40
|
-
this.externalChannel = externalChannel;
|
|
10
|
+
constructor(externalEventBus, eventMessageValidator, logger) {
|
|
41
11
|
this.externalEventBus = externalEventBus;
|
|
42
|
-
this.externalEventListenerList = externalEventListenerList;
|
|
43
12
|
this.eventMessageValidator = eventMessageValidator;
|
|
44
13
|
this.logger = logger;
|
|
45
|
-
this.pubsub = pubsub;
|
|
46
|
-
this.timeout = timeout;
|
|
47
|
-
}
|
|
48
|
-
onRegistrationsFinished() {
|
|
49
|
-
this.eventListeners = Utils.collectEventBusListeners(this.externalEventListenerList);
|
|
50
14
|
}
|
|
51
|
-
async receiveDirect(event, request = {}, ops
|
|
52
|
-
const requestUuid = (0,
|
|
15
|
+
async receiveDirect(event, request = {}, ops) {
|
|
16
|
+
const { exchangeName, validationRules, ignoreCache, retry, timeout, registryApiVersion, requestUuid = (0, node_crypto_1.randomUUID)() } = ops;
|
|
53
17
|
const payload = { uuid: requestUuid, request };
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
18
|
+
const options = { ignoreCache, retry, timeout, exchangeName, registryApiVersion };
|
|
19
|
+
const externalResponse = await this.externalEventBus.publishDirect(event, payload, options);
|
|
20
|
+
this.logger.debug('External direct response', externalResponse);
|
|
57
21
|
if (validationRules) {
|
|
58
22
|
try {
|
|
59
23
|
this.eventMessageValidator.validateSyncedEventMessage(externalResponse, validationRules);
|
|
60
24
|
}
|
|
61
25
|
catch (err) {
|
|
62
26
|
const errorMsg = 'Message in a wrong format was received from a direct channel';
|
|
63
|
-
this.logger.
|
|
27
|
+
this.logger.error(errorMsg, { err, externalResponse });
|
|
64
28
|
throw new Error(errorMsg);
|
|
65
29
|
}
|
|
66
30
|
}
|
|
67
31
|
const { error, response } = externalResponse.payload;
|
|
68
32
|
if (error) {
|
|
69
|
-
this.logger.
|
|
70
|
-
|
|
33
|
+
this.logger.error(`Error received by an external event ${event}: ${error.http_code} ${error.message}`, { requestUuid });
|
|
34
|
+
const errMsg = error.message || 'unknown error';
|
|
35
|
+
throw new errors_1.ExternalCommunicatorError(errMsg, error.http_code, { event, httpCode: error.http_code, ...error.data });
|
|
71
36
|
}
|
|
72
37
|
return response;
|
|
73
38
|
}
|
|
74
|
-
/**
|
|
75
|
-
* @deprecated use receiveDirect in case provider supports direct communcation
|
|
76
|
-
*/
|
|
77
|
-
async receive(event, request = {}, ops = {}) {
|
|
78
|
-
const timeout = ops.timeout || this.timeout;
|
|
79
|
-
const eventListener = this.eventListeners[event];
|
|
80
|
-
if (!eventListener) {
|
|
81
|
-
throw new Error(`Listener not found by the provided event: ${event}`);
|
|
82
|
-
}
|
|
83
|
-
if (!eventListener.isSync) {
|
|
84
|
-
throw new Error(`Listener is not synchronous for the provided event: ${event}`);
|
|
85
|
-
}
|
|
86
|
-
const requestUuid = ops.requestUuid || (0, uuid_1.v4)();
|
|
87
|
-
const payload = { uuid: requestUuid, request };
|
|
88
|
-
const { ignoreCache, retry, async } = ops;
|
|
89
|
-
const options = { ignoreCache, retry };
|
|
90
|
-
if (async) {
|
|
91
|
-
const success = await this.externalEventBus.publish(event, payload, options);
|
|
92
|
-
if (!success) {
|
|
93
|
-
throw new Error(`Failed to publish async external event ${event}`);
|
|
94
|
-
}
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const channel = this.externalChannel.getChannel(event, requestUuid);
|
|
98
|
-
const promise = new Promise((resolve, reject) => {
|
|
99
|
-
const timer = setTimeout(async () => {
|
|
100
|
-
await this.pubsub.unsubscribe(channel);
|
|
101
|
-
return reject(new Error(`External communication timeout error for the channel ${channel}`));
|
|
102
|
-
}, timeout);
|
|
103
|
-
this.pubsub
|
|
104
|
-
.onceChannelMessage(channel, async (message) => {
|
|
105
|
-
clearTimeout(timer);
|
|
106
|
-
const data = JSON.parse(message);
|
|
107
|
-
try {
|
|
108
|
-
this.eventMessageValidator.validateSyncedEventMessage(data, eventListener.validationRules);
|
|
109
|
-
}
|
|
110
|
-
catch (err) {
|
|
111
|
-
const errorMsg = 'Message in a wrong format was received from a redis channel';
|
|
112
|
-
this.logger.fatal(errorMsg, { err, data });
|
|
113
|
-
return reject(new Error(errorMsg));
|
|
114
|
-
}
|
|
115
|
-
const { error, response } = data.payload;
|
|
116
|
-
if (error) {
|
|
117
|
-
this.logger.fatal(`Error received by an external event ${event}: ${error.http_code} ${error.message}`, request);
|
|
118
|
-
return reject(new errors_1.ExternalCommunicatorError(error.message, error.http_code, { event, ...error.data }));
|
|
119
|
-
}
|
|
120
|
-
return resolve(response);
|
|
121
|
-
})
|
|
122
|
-
.catch(reject);
|
|
123
|
-
});
|
|
124
|
-
await this.externalChannel.saveActiveChannel(channel, timeout);
|
|
125
|
-
const success = await this.externalEventBus.publish(event, payload);
|
|
126
|
-
if (!success) {
|
|
127
|
-
await this.pubsub.unsubscribe(channel);
|
|
128
|
-
throw new Error(`Failed to publish external event ${event}`);
|
|
129
|
-
}
|
|
130
|
-
return await promise;
|
|
131
|
-
}
|
|
132
39
|
}
|
|
133
40
|
exports.ExternalCommunicator = ExternalCommunicator;
|
|
134
41
|
//# sourceMappingURL=externalCommunicator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"externalCommunicator.js","sourceRoot":"","sources":["../../src/services/externalCommunicator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"externalCommunicator.js","sourceRoot":"","sources":["../../src/services/externalCommunicator.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AAGxC,iDAAgE;AAMhE,MAAa,oBAAoB;IAER;IACA;IACA;IAHrB,YACqB,gBAAuC,EACvC,qBAA4C,EAC5C,MAAc;QAFd,qBAAgB,GAAhB,gBAAgB,CAAuB;QACvC,0BAAqB,GAArB,qBAAqB,CAAuB;QAC5C,WAAM,GAAN,MAAM,CAAQ;IAChC,CAAC;IAEJ,KAAK,CAAC,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,CAAC,OAAO,IAAI,eAAe,CAAA;YAE/C,MAAM,IAAI,kCAAyB,CAAC,MAAM,EAAE,KAAK,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,EAAE,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,CAAA;QACrH,CAAC;QAED,OAAO,QAAQ,CAAA;IACnB,CAAC;CACJ;AA3CD,oDA2CC"}
|
|
@@ -1,70 +1,162 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
4
|
};
|
|
25
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
6
|
exports.ExternalEventBus = void 0;
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
7
|
+
const errors_1 = require("@diia-inhouse/errors");
|
|
8
|
+
const types_1 = require("@diia-inhouse/types");
|
|
9
|
+
const constants_1 = __importDefault(require("../constants"));
|
|
10
|
+
const interfaces_1 = require("../interfaces");
|
|
11
|
+
const communicator_1 = __importDefault(require("./communicator"));
|
|
12
|
+
const eventCommunicator_1 = require("./eventCommunicator");
|
|
13
|
+
class ExternalEventBus extends communicator_1.default {
|
|
32
14
|
envService;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
15
|
+
eventCommunicator;
|
|
16
|
+
publishEventsSet;
|
|
17
|
+
subscribeEventsSet;
|
|
18
|
+
exchangePrefixName = 'TopicExternal';
|
|
19
|
+
eventQueueMap = new Map();
|
|
20
|
+
constructor(logger, systemServiceName, envService, queueProvider, externalEventListenerList, eventMessageHandler, hostName) {
|
|
21
|
+
super(logger, queueProvider, hostName, systemServiceName);
|
|
38
22
|
this.envService = envService;
|
|
39
|
-
|
|
23
|
+
const { rabbit: { custom }, service: { publish = [], subscribe = [] }, } = this.queueProvider.getConfig();
|
|
24
|
+
if (custom?.responseRoutingKeyPrefix && !this.envService.isLocal() && !this.envService.isTest()) {
|
|
25
|
+
throw new Error('Response routing key could be used only on local env');
|
|
26
|
+
}
|
|
27
|
+
this.publishEventsSet = new Set(publish);
|
|
28
|
+
this.subscribeEventsSet = new Set(subscribe);
|
|
29
|
+
this.eventQueueMap = this.buildEventQueueMap();
|
|
30
|
+
this.eventCommunicator = new eventCommunicator_1.EventCommunicator(logger, queueProvider, eventMessageHandler, externalEventListenerList);
|
|
40
31
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
for (const listener of this.externalEventListenerList) {
|
|
52
|
-
this.logger.info(`External event listener [${listener.event}] initialized successfully`);
|
|
53
|
-
}
|
|
32
|
+
static BuildRequestRoutingKey(eventName) {
|
|
33
|
+
return `queue.${constants_1.default.PROJECT_NAME}.${eventName}.req`;
|
|
34
|
+
}
|
|
35
|
+
static BuildResponseRoutingKey(eventName, portalEvents = []) {
|
|
36
|
+
const queuePrefix = ExternalEventBus.prepareQueuePrefix(eventName, portalEvents);
|
|
37
|
+
return `${queuePrefix}.${eventName}.res`;
|
|
38
|
+
}
|
|
39
|
+
static prepareQueuePrefix(event, portalEvents = []) {
|
|
40
|
+
if (portalEvents.includes(event)) {
|
|
41
|
+
return `queue.${constants_1.default.PORTAL_NAME}`;
|
|
54
42
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
return `queue.${constants_1.default.PROJECT_NAME}`;
|
|
44
|
+
}
|
|
45
|
+
async publish(eventName, payload, options) {
|
|
46
|
+
const message = this.getPublishMessage(eventName, payload, options);
|
|
47
|
+
const routingKey = this.getPublishRoutingKey(eventName);
|
|
48
|
+
return await this.publishEventToExchange(eventName, message, { ...options, routingKey });
|
|
49
|
+
}
|
|
50
|
+
async publishDirect(eventName, payload, options = {}) {
|
|
51
|
+
const { exchangeName: customExchangeName } = options;
|
|
52
|
+
const exchangeName = customExchangeName ?? this.eventNameToExchangeNameMap.get(eventName);
|
|
53
|
+
if (!exchangeName) {
|
|
54
|
+
throw new errors_1.ExternalCommunicatorError(`Exchange name for event ${eventName} is not defined`, types_1.HttpStatusCode.INTERNAL_SERVER_ERROR, { event: eventName, customExchangeName }, errors_1.ErrorType.Unoperated);
|
|
58
55
|
}
|
|
56
|
+
const routingKey = ExternalEventBus.BuildRequestRoutingKey(eventName);
|
|
57
|
+
const { data } = this.getPublishMessage(eventName, payload, options);
|
|
58
|
+
return await this.queueProvider.publishExternalDirect(data, exchangeName, routingKey, options);
|
|
59
|
+
}
|
|
60
|
+
getUnicastListeners() {
|
|
61
|
+
return this.eventCommunicator.getUnicastListeners();
|
|
62
|
+
}
|
|
63
|
+
getExchangeNameWithSuffix(topic) {
|
|
64
|
+
return `${this.exchangePrefixName}${topic}`;
|
|
59
65
|
}
|
|
60
|
-
|
|
61
|
-
|
|
66
|
+
getProducerExchangesOptions() {
|
|
67
|
+
const [exchangesOptions] = this.defineQueuesAndExchangesOptionsBasedOnGlobalConfig();
|
|
68
|
+
return exchangesOptions;
|
|
62
69
|
}
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
getMulticastListeners() {
|
|
71
|
+
const [exchangesOptions, queuesOptions] = this.defineQueuesAndExchangesOptionsBasedOnGlobalConfig();
|
|
72
|
+
return this.eventCommunicator.getMulticastListeners(queuesOptions, exchangesOptions, this.eventQueueMap);
|
|
65
73
|
}
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
getPublishMessage(eventName, message, options) {
|
|
75
|
+
const { rabbit: { custom: { responseRoutingKeyPrefix } = {} }, } = this.queueProvider.getConfig();
|
|
76
|
+
const responseRoutingKey = this.buildResponseQueueName(eventName, responseRoutingKeyPrefix);
|
|
77
|
+
const partialMeta = {
|
|
78
|
+
ignoreCache: options?.ignoreCache,
|
|
79
|
+
registryApiVersion: options?.registryApiVersion,
|
|
80
|
+
...(this.publishEventsSet.has(eventName) ? { responseRoutingKey } : {}),
|
|
81
|
+
};
|
|
82
|
+
const data = this.getPublishQueueMessageData(eventName, message, partialMeta);
|
|
83
|
+
return new interfaces_1.Message(data);
|
|
84
|
+
}
|
|
85
|
+
getPublishRoutingKey(eventName) {
|
|
86
|
+
if (this.publishEventsSet.has(eventName)) {
|
|
87
|
+
return ExternalEventBus.BuildRequestRoutingKey(eventName);
|
|
88
|
+
}
|
|
89
|
+
else if (this.subscribeEventsSet.has(eventName)) {
|
|
90
|
+
const { portalEvents } = this.queueProvider.getConfig();
|
|
91
|
+
return ExternalEventBus.BuildResponseRoutingKey(eventName, portalEvents);
|
|
92
|
+
}
|
|
93
|
+
return '';
|
|
94
|
+
}
|
|
95
|
+
defineQueuesAndExchangesOptionsBasedOnGlobalConfig() {
|
|
96
|
+
const { rabbit: { listenerOptions, declareOptions: { assertQueues, assertExchanges, queuesOptions: overriddenQueuesOptions = {} } = {}, }, } = this.queueProvider.getConfig();
|
|
97
|
+
const queuesOptions = [];
|
|
98
|
+
const exchangesMap = new Map();
|
|
99
|
+
for (const [eventName, queueName] of this.eventQueueMap.entries()) {
|
|
100
|
+
const exchangeName = this.eventNameToExchangeNameMap.get(eventName);
|
|
101
|
+
if (!exchangeName) {
|
|
102
|
+
this.logger.error(`Can't find external topic name for event [${eventName}]`);
|
|
103
|
+
return [[], []];
|
|
104
|
+
}
|
|
105
|
+
const queueOptions = {
|
|
106
|
+
name: queueName,
|
|
107
|
+
declare: assertQueues,
|
|
108
|
+
type: interfaces_1.QueueTypes.Quorum,
|
|
109
|
+
options: listenerOptions?.queueOptions,
|
|
110
|
+
bindTo: [
|
|
111
|
+
{
|
|
112
|
+
bind: assertQueues,
|
|
113
|
+
routingKey: queueName,
|
|
114
|
+
exchangeName: exchangeName,
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
consumerOptions: this.optionsBuilder.defineConsumerOptionsBasedOnGlobalConfig(),
|
|
118
|
+
...overriddenQueuesOptions,
|
|
119
|
+
};
|
|
120
|
+
queuesOptions.push(queueOptions);
|
|
121
|
+
const exchangeOptions = {
|
|
122
|
+
name: exchangeName,
|
|
123
|
+
type: interfaces_1.ExchangeType.Topic,
|
|
124
|
+
declare: assertExchanges,
|
|
125
|
+
};
|
|
126
|
+
exchangesMap.set(exchangeName, exchangeOptions);
|
|
127
|
+
}
|
|
128
|
+
const exchangesOptions = [...exchangesMap.values()];
|
|
129
|
+
return [exchangesOptions, queuesOptions];
|
|
130
|
+
}
|
|
131
|
+
buildEventQueueMap() {
|
|
132
|
+
const eventQueueMap = new Map();
|
|
133
|
+
const { service: { publish = [], subscribe = [] }, rabbit: { custom: { responseRoutingKeyPrefix } = {} }, } = this.queueProvider.getConfig();
|
|
134
|
+
if (publish.length === 0 && subscribe.length === 0) {
|
|
135
|
+
this.logger.info('No one external events to listen');
|
|
136
|
+
return eventQueueMap;
|
|
137
|
+
}
|
|
138
|
+
for (const responseEvent of publish) {
|
|
139
|
+
eventQueueMap.set(responseEvent, this.buildResponseQueueName(responseEvent, responseRoutingKeyPrefix));
|
|
140
|
+
}
|
|
141
|
+
for (const requestEvent of subscribe) {
|
|
142
|
+
eventQueueMap.set(requestEvent, this.buildRequestQueueName(requestEvent, responseRoutingKeyPrefix));
|
|
143
|
+
}
|
|
144
|
+
return eventQueueMap;
|
|
145
|
+
}
|
|
146
|
+
buildResponseQueueName(eventName, prefix) {
|
|
147
|
+
const result = `queue.${constants_1.default.PROJECT_NAME}.${eventName}.res`;
|
|
148
|
+
if (prefix) {
|
|
149
|
+
return `${prefix}.${result}`;
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
buildRequestQueueName(eventName, prefix) {
|
|
154
|
+
const { portalEvents } = this.queueProvider.getConfig();
|
|
155
|
+
const result = `${ExternalEventBus.prepareQueuePrefix(eventName, portalEvents)}.${eventName}.req`;
|
|
156
|
+
if (prefix) {
|
|
157
|
+
return `${prefix}.${result}`;
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
68
160
|
}
|
|
69
161
|
}
|
|
70
162
|
exports.ExternalEventBus = ExternalEventBus;
|