@diia-inhouse/diia-queue 7.3.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.
Files changed (90) hide show
  1. package/LICENCE.md +287 -0
  2. package/README.md +93 -0
  3. package/dist/constants.js +7 -0
  4. package/dist/constants.js.map +1 -0
  5. package/dist/index.js +19 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/interfaces/deps.js +3 -0
  8. package/dist/interfaces/deps.js.map +1 -0
  9. package/dist/interfaces/externalCommunicator.js +3 -0
  10. package/dist/interfaces/externalCommunicator.js.map +1 -0
  11. package/dist/interfaces/index.js +27 -0
  12. package/dist/interfaces/index.js.map +1 -0
  13. package/dist/interfaces/metrics/index.js +3 -0
  14. package/dist/interfaces/metrics/index.js.map +1 -0
  15. package/dist/interfaces/providers/rabbitmq/amqpConnection.js +14 -0
  16. package/dist/interfaces/providers/rabbitmq/amqpConnection.js.map +1 -0
  17. package/dist/interfaces/providers/rabbitmq/amqpListener.js +3 -0
  18. package/dist/interfaces/providers/rabbitmq/amqpListener.js.map +1 -0
  19. package/dist/interfaces/providers/rabbitmq/amqpPublisher.js +12 -0
  20. package/dist/interfaces/providers/rabbitmq/amqpPublisher.js.map +1 -0
  21. package/dist/interfaces/providers/rabbitmq/index.js +25 -0
  22. package/dist/interfaces/providers/rabbitmq/index.js.map +1 -0
  23. package/dist/interfaces/queueConfig/configs.js +9 -0
  24. package/dist/interfaces/queueConfig/configs.js.map +1 -0
  25. package/dist/interfaces/queueConfig/index.js +18 -0
  26. package/dist/interfaces/queueConfig/index.js.map +1 -0
  27. package/dist/interfaces/queueStatus.js +3 -0
  28. package/dist/interfaces/queueStatus.js.map +1 -0
  29. package/dist/metrics/index.js +9 -0
  30. package/dist/metrics/index.js.map +1 -0
  31. package/dist/providers/rabbitmq/amqpConnection.js +129 -0
  32. package/dist/providers/rabbitmq/amqpConnection.js.map +1 -0
  33. package/dist/providers/rabbitmq/amqpListener.js +171 -0
  34. package/dist/providers/rabbitmq/amqpListener.js.map +1 -0
  35. package/dist/providers/rabbitmq/amqpPublisher.js +140 -0
  36. package/dist/providers/rabbitmq/amqpPublisher.js.map +1 -0
  37. package/dist/providers/rabbitmq/index.js +367 -0
  38. package/dist/providers/rabbitmq/index.js.map +1 -0
  39. package/dist/services/eventBus.js +63 -0
  40. package/dist/services/eventBus.js.map +1 -0
  41. package/dist/services/eventMessageHandler.js +145 -0
  42. package/dist/services/eventMessageHandler.js.map +1 -0
  43. package/dist/services/eventMessageValidator.js +44 -0
  44. package/dist/services/eventMessageValidator.js.map +1 -0
  45. package/dist/services/externalCommunicator.js +134 -0
  46. package/dist/services/externalCommunicator.js.map +1 -0
  47. package/dist/services/externalCommunicatorChannel.js +31 -0
  48. package/dist/services/externalCommunicatorChannel.js.map +1 -0
  49. package/dist/services/externalEventBus.js +71 -0
  50. package/dist/services/externalEventBus.js.map +1 -0
  51. package/dist/services/index.js +26 -0
  52. package/dist/services/index.js.map +1 -0
  53. package/dist/services/queue.js +56 -0
  54. package/dist/services/queue.js.map +1 -0
  55. package/dist/services/scheduledTask.js +65 -0
  56. package/dist/services/scheduledTask.js.map +1 -0
  57. package/dist/services/task.js +56 -0
  58. package/dist/services/task.js.map +1 -0
  59. package/dist/types/constants.d.ts +5 -0
  60. package/dist/types/index.d.ts +3 -0
  61. package/dist/types/interfaces/deps.d.ts +17 -0
  62. package/dist/types/interfaces/externalCommunicator.d.ts +35 -0
  63. package/dist/types/interfaces/index.d.ts +89 -0
  64. package/dist/types/interfaces/metrics/index.d.ts +7 -0
  65. package/dist/types/interfaces/providers/rabbitmq/amqpConnection.d.ts +26 -0
  66. package/dist/types/interfaces/providers/rabbitmq/amqpListener.d.ts +7 -0
  67. package/dist/types/interfaces/providers/rabbitmq/amqpPublisher.d.ts +24 -0
  68. package/dist/types/interfaces/providers/rabbitmq/index.d.ts +50 -0
  69. package/dist/types/interfaces/queueConfig/configs.d.ts +39 -0
  70. package/dist/types/interfaces/queueConfig/index.d.ts +1 -0
  71. package/dist/types/interfaces/queueStatus.d.ts +6 -0
  72. package/dist/types/metrics/index.d.ts +4 -0
  73. package/dist/types/providers/rabbitmq/amqpConnection.d.ts +24 -0
  74. package/dist/types/providers/rabbitmq/amqpListener.d.ts +26 -0
  75. package/dist/types/providers/rabbitmq/amqpPublisher.d.ts +25 -0
  76. package/dist/types/providers/rabbitmq/index.d.ts +55 -0
  77. package/dist/types/services/eventBus.d.ts +16 -0
  78. package/dist/types/services/eventMessageHandler.d.ts +21 -0
  79. package/dist/types/services/eventMessageValidator.d.ts +9 -0
  80. package/dist/types/services/externalCommunicator.d.ts +24 -0
  81. package/dist/types/services/externalCommunicatorChannel.d.ts +11 -0
  82. package/dist/types/services/externalEventBus.d.ts +18 -0
  83. package/dist/types/services/index.d.ts +9 -0
  84. package/dist/types/services/queue.d.ts +18 -0
  85. package/dist/types/services/scheduledTask.d.ts +17 -0
  86. package/dist/types/services/task.d.ts +18 -0
  87. package/dist/types/utils.d.ts +3 -0
  88. package/dist/utils.js +12 -0
  89. package/dist/utils.js.map +1 -0
  90. package/package.json +105 -0
@@ -0,0 +1,134 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ExternalCommunicator = void 0;
27
+ const uuid_1 = require("uuid");
28
+ const errors_1 = require("@diia-inhouse/errors");
29
+ const Utils = __importStar(require("../utils"));
30
+ class ExternalCommunicator {
31
+ externalChannel;
32
+ externalEventBus;
33
+ externalEventListenerList;
34
+ eventMessageValidator;
35
+ logger;
36
+ pubsub;
37
+ timeout;
38
+ eventListeners;
39
+ constructor(externalChannel, externalEventBus, externalEventListenerList, eventMessageValidator, logger, pubsub, timeout = 10000) {
40
+ this.externalChannel = externalChannel;
41
+ this.externalEventBus = externalEventBus;
42
+ this.externalEventListenerList = externalEventListenerList;
43
+ this.eventMessageValidator = eventMessageValidator;
44
+ this.logger = logger;
45
+ this.pubsub = pubsub;
46
+ this.timeout = timeout;
47
+ }
48
+ onRegistrationsFinished() {
49
+ this.eventListeners = Utils.collectEventBusListeners(this.externalEventListenerList);
50
+ }
51
+ async receiveDirect(event, request = {}, ops = {}) {
52
+ const requestUuid = (0, uuid_1.v4)();
53
+ const payload = { uuid: requestUuid, request };
54
+ const { topic, validationRules, ignoreCache, retry, timeout } = ops;
55
+ const options = { ignoreCache, retry, timeout };
56
+ const externalResponse = await this.externalEventBus.publishDirect(event, payload, topic, options);
57
+ if (validationRules) {
58
+ try {
59
+ this.eventMessageValidator.validateSyncedEventMessage(externalResponse, validationRules);
60
+ }
61
+ catch (err) {
62
+ const errorMsg = 'Message in a wrong format was received from a direct channel';
63
+ this.logger.fatal(errorMsg, { err, externalResponse });
64
+ throw new Error(errorMsg);
65
+ }
66
+ }
67
+ const { error, response } = externalResponse.payload;
68
+ if (error) {
69
+ this.logger.fatal(`Error received by an external event ${event}: ${error.http_code} ${error.message}`, request);
70
+ throw new errors_1.ExternalCommunicatorError(error.message, error.http_code, { event, httpCode: error.http_code, ...error.data });
71
+ }
72
+ return response;
73
+ }
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
+ }
133
+ exports.ExternalCommunicator = ExternalCommunicator;
134
+ //# sourceMappingURL=externalCommunicator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"externalCommunicator.js","sourceRoot":"","sources":["../../src/services/externalCommunicator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAAiC;AAEjC,iDAAgE;AAOhE,gDAAiC;AAKjC,MAAa,oBAAoB;IAIR;IACA;IACA;IACA;IAEA;IACA;IACA;IAVb,cAAc,CAAgB;IAEtC,YACqB,eAA4C,EAC5C,gBAAuC,EACvC,yBAA6C,EAC7C,qBAA4C,EAE5C,MAAc,EACd,MAAqB,EACrB,UAAU,KAAK;QAPf,oBAAe,GAAf,eAAe,CAA6B;QAC5C,qBAAgB,GAAhB,gBAAgB,CAAuB;QACvC,8BAAyB,GAAzB,yBAAyB,CAAoB;QAC7C,0BAAqB,GAArB,qBAAqB,CAAuB;QAE5C,WAAM,GAAN,MAAM,CAAQ;QACd,WAAM,GAAN,MAAM,CAAe;QACrB,YAAO,GAAP,OAAO,CAAQ;IACjC,CAAC;IAEJ,uBAAuB;QACnB,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,wBAAwB,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAA;IACxF,CAAC;IAED,KAAK,CAAC,aAAa,CAAI,KAAa,EAAE,UAAmB,EAAE,EAAE,MAAwB,EAAE;QACnF,MAAM,WAAW,GAAW,IAAA,SAAI,GAAE,CAAA;QAClC,MAAM,OAAO,GAAmB,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,CAAA;QAC9D,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,GAAG,CAAA;QACnE,MAAM,OAAO,GAAyB,EAAE,WAAW,EAAE,KAAK,EA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,CAAC,oCAAoC,KAAK,EAAE,CAAC,CAAA;SAC/D;QAED,OAAO,MAAM,OAAO,CAAA;IACxB,CAAC;CACJ;AA5HD,oDA4HC"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExternalCommunicatorChannel = void 0;
4
+ class ExternalCommunicatorChannel {
5
+ cache;
6
+ constructor(cache) {
7
+ this.cache = cache;
8
+ }
9
+ getChannel(event, requestUuid) {
10
+ this.assertChannelAvailability();
11
+ return `external_communicator_channel_${event}_${requestUuid}`;
12
+ }
13
+ async isChannelActive(channel) {
14
+ this.assertChannelAvailability();
15
+ return Boolean(await this.cache.get(this.getActiveChannelKey(channel)));
16
+ }
17
+ async saveActiveChannel(channel, timeout) {
18
+ this.assertChannelAvailability();
19
+ await this.cache.set(this.getActiveChannelKey(channel), 'true', Math.floor(timeout / 1000));
20
+ }
21
+ getActiveChannelKey(channel) {
22
+ return `active_${channel}`;
23
+ }
24
+ assertChannelAvailability() {
25
+ if (!this.cache) {
26
+ throw new Error('Cache service is not provided for the ExternalCommunicatorChannel service');
27
+ }
28
+ }
29
+ }
30
+ exports.ExternalCommunicatorChannel = ExternalCommunicatorChannel;
31
+ //# sourceMappingURL=externalCommunicatorChannel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"externalCommunicatorChannel.js","sourceRoot":"","sources":["../../src/services/externalCommunicatorChannel.ts"],"names":[],"mappings":";;;AAIA,MAAa,2BAA2B;IACP;IAA7B,YAA6B,KAA+B;QAA/B,UAAK,GAAL,KAAK,CAA0B;IAAG,CAAC;IAEhE,UAAU,CAAC,KAAgB,EAAE,WAAmB;QAC5C,IAAI,CAAC,yBAAyB,EAAE,CAAA;QAEhC,OAAO,iCAAiC,KAAK,IAAI,WAAW,EAAE,CAAA;IAClE,CAAC;IAED,KAAK,CAAC,eAAe,CAAC,OAAe;QACjC,IAAI,CAAC,yBAAyB,EAAE,CAAA;QAEhC,OAAO,OAAO,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;IAC3E,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,OAAe,EAAE,OAAe;QACpD,IAAI,CAAC,yBAAyB,EAAE,CAAA;QAEhC,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC,CAAA;IAC/F,CAAC;IAEO,mBAAmB,CAAC,OAAe;QACvC,OAAO,UAAU,OAAO,EAAE,CAAA;IAC9B,CAAC;IAEO,yBAAyB;QAC7B,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE;YACb,MAAM,IAAI,KAAK,CAAC,2EAA2E,CAAC,CAAA;SAC/F;IACL,CAAC;CACJ;AA9BD,kEA8BC"}
@@ -0,0 +1,71 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ExternalEventBus = void 0;
27
+ const Utils = __importStar(require("../utils"));
28
+ class ExternalEventBus {
29
+ queueProvider;
30
+ externalEventListenerList;
31
+ eventMessageHandler;
32
+ envService;
33
+ logger;
34
+ constructor(queueProvider, externalEventListenerList, eventMessageHandler, envService, logger) {
35
+ this.queueProvider = queueProvider;
36
+ this.externalEventListenerList = externalEventListenerList;
37
+ this.eventMessageHandler = eventMessageHandler;
38
+ this.envService = envService;
39
+ this.logger = logger;
40
+ }
41
+ async onInit() {
42
+ const externalConfig = this.queueProvider.getConfig();
43
+ try {
44
+ if (externalConfig.custom?.responseRoutingKeyPrefix && !this.envService.isLocal() && !this.envService.isTest()) {
45
+ throw new Error('Response routing key could be used only on local env');
46
+ }
47
+ const eventListeners = Utils.collectEventBusListeners(this.externalEventListenerList);
48
+ await this.subscribe(this.eventMessageHandler.eventListenersMessageHandler.bind(this.eventMessageHandler, eventListeners), {
49
+ listener: externalConfig.listenerOptions,
50
+ });
51
+ for (const listener of this.externalEventListenerList) {
52
+ this.logger.info(`External event listener [${listener.event}] initialized successfully`);
53
+ }
54
+ }
55
+ catch (err) {
56
+ this.logger.error('Failed to initialize external event bus', { err });
57
+ throw err;
58
+ }
59
+ }
60
+ async subscribe(messageHandler, options) {
61
+ return await this.queueProvider.subscribeExternal(messageHandler, options);
62
+ }
63
+ async publish(eventName, message, options) {
64
+ return await this.queueProvider.publishExternal(eventName, message, options);
65
+ }
66
+ async publishDirect(eventName, message, topic, options) {
67
+ return await this.queueProvider.publishExternalDirect(eventName, message, topic, options);
68
+ }
69
+ }
70
+ exports.ExternalEventBus = ExternalEventBus;
71
+ //# sourceMappingURL=externalEventBus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"externalEventBus.js","sourceRoot":"","sources":["../../src/services/externalEventBus.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;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AK,CAAC,SAAS,CAAC,cAA8B,EAAE,OAA0B;QACtE,OAAO,MAAM,IAAI,CAAC,aAAa,CAAC,iBAAiB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAA;IAC9E,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,SAAoB,EAAE,OAAuB,EAAE,OAAqC;QAC9F,OAAO,MAAM,IAAI,CAAC,aAAa,CAAC,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;IAChF,CAAC;IAED,KAAK,CAAC,aAAa,CAAI,SAAoB,EAAE,OAAuB,EAAE,KAAa,EAAE,OAA8B;QAC/G,OAAO,MAAM,IAAI,CAAC,aAAa,CAAC,qBAAqB,CAAC,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;IAC7F,CAAC;CACJ;AA3CD,4CA2CC"}
@@ -0,0 +1,26 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./eventBus"), exports);
18
+ __exportStar(require("./eventMessageHandler"), exports);
19
+ __exportStar(require("./eventMessageValidator"), exports);
20
+ __exportStar(require("./externalCommunicator"), exports);
21
+ __exportStar(require("./externalCommunicatorChannel"), exports);
22
+ __exportStar(require("./externalEventBus"), exports);
23
+ __exportStar(require("./scheduledTask"), exports);
24
+ __exportStar(require("./task"), exports);
25
+ __exportStar(require("./queue"), exports);
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA0B;AAE1B,wDAAqC;AAErC,0DAAuC;AAEvC,yDAAsC;AAEtC,gEAA6C;AAE7C,qDAAkC;AAElC,kDAA+B;AAE/B,yCAAsB;AAEtB,0CAAuB"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Queue = void 0;
4
+ const types_1 = require("@diia-inhouse/types");
5
+ const interfaces_1 = require("../interfaces");
6
+ const queueConfig_1 = require("../interfaces/queueConfig");
7
+ const rabbitmq_1 = require("../providers/rabbitmq");
8
+ class Queue {
9
+ serviceName;
10
+ connectionConfig;
11
+ asyncLocalStorage;
12
+ logger;
13
+ internalQueue;
14
+ externalQueue;
15
+ constructor(serviceName, connectionConfig, asyncLocalStorage, logger) {
16
+ this.serviceName = serviceName;
17
+ this.connectionConfig = connectionConfig;
18
+ this.asyncLocalStorage = asyncLocalStorage;
19
+ this.logger = logger;
20
+ }
21
+ async onHealthCheck() {
22
+ const internalQueueStatus = this.internalQueue?.getStatus();
23
+ const externalQueueStatus = this.externalQueue?.getStatus();
24
+ const queueStatuses = [internalQueueStatus, externalQueueStatus].filter(Boolean).flatMap((status) => Object.values(status));
25
+ const status = queueStatuses.some((s) => s !== interfaces_1.ConnectionStatus.Connected)
26
+ ? types_1.HttpStatusCode.SERVICE_UNAVAILABLE
27
+ : types_1.HttpStatusCode.OK;
28
+ return {
29
+ status,
30
+ details: {
31
+ rabbit: {
32
+ internal: internalQueueStatus,
33
+ external: externalQueueStatus,
34
+ },
35
+ },
36
+ };
37
+ }
38
+ getInternalQueue() {
39
+ if (this.internalQueue) {
40
+ return this.internalQueue;
41
+ }
42
+ const { internal, serviceRulesConfig: { queuesConfig, servicesConfig, topicsConfig, internalEvents }, } = this.connectionConfig;
43
+ this.internalQueue = new rabbitmq_1.RabbitMQProvider(this.serviceName, internal, servicesConfig[queueConfig_1.QueueConfigType.Internal], topicsConfig[queueConfig_1.QueueConfigType.Internal], [], internalEvents, queueConfig_1.QueueConfigType.Internal, this.logger, this.asyncLocalStorage, queuesConfig[queueConfig_1.QueueConfigType.Internal]);
44
+ return this.internalQueue;
45
+ }
46
+ getExternalQueue() {
47
+ if (this.externalQueue) {
48
+ return this.externalQueue;
49
+ }
50
+ const { external, serviceRulesConfig: { portalEvents, servicesConfig, topicsConfig, internalEvents }, } = this.connectionConfig;
51
+ this.externalQueue = new rabbitmq_1.RabbitMQProvider(this.serviceName, external, servicesConfig[queueConfig_1.QueueConfigType.External], topicsConfig[queueConfig_1.QueueConfigType.External], portalEvents, internalEvents, queueConfig_1.QueueConfigType.External, this.logger, this.asyncLocalStorage);
52
+ return this.externalQueue;
53
+ }
54
+ }
55
+ exports.Queue = Queue;
56
+ //# sourceMappingURL=queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/services/queue.ts"],"names":[],"mappings":";;;AAEA,+CAA8F;AAE9F,8CAAqF;AACrF,2DAA2D;AAE3D,oDAAwD;AAExD,MAAa,KAAK;IAMO;IACA;IAEA;IACA;IATb,aAAa,CAAkB;IAE/B,aAAa,CAAkB;IAEvC,YACqB,WAAmB,EACnB,gBAAuC,EAEvC,iBAAkD,EAClD,MAAc;QAJd,gBAAW,GAAX,WAAW,CAAQ;QACnB,qBAAgB,GAAhB,gBAAgB,CAAuB;QAEvC,sBAAiB,GAAjB,iBAAiB,CAAiC;QAClD,WAAM,GAAN,MAAM,CAAQ;IAChC,CAAC;IAEJ,KAAK,CAAC,aAAa;QACf,MAAM,mBAAmB,GAAG,IAAI,CAAC,aAAa,EAAE,SAAS,EAAE,CAAA;QAC3D,MAAM,mBAAmB,GAAG,IAAI,CA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OAAO,IAAI,CAAC,aAAa,CAAA;SAC5B;QAED,MAAM,EACF,QAAQ,EACR,kBAAkB,EAAE,EAAE,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,cAAc,EAAE,GACrF,GAAG,IAAI,CAAC,gBAAgB,CAAA;QAEzB,IAAI,CAAC,aAAa,GAAG,IAAI,2BAAgB,CACrC,IAAI,CAAC,WAAW,EAChB,QAAQ,EACR,cAAc,CAAC,6BAAe,CAAC,QAAQ,CAAC,EACxC,YAAY,CAAC,6BAAe,CAAC,QAAQ,CAAC,EACtC,YAAY,EACZ,cAAc,EACd,6BAAe,CAAC,QAAQ,EACxB,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,iBAAiB,CACzB,CAAA;QAED,OAAO,IAAI,CAAC,aAAa,CAAA;IAC7B,CAAC;CACJ;AApFD,sBAoFC"}
@@ -0,0 +1,65 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ScheduledTask = void 0;
27
+ const Utils = __importStar(require("../utils"));
28
+ class ScheduledTask {
29
+ queueProvider;
30
+ scheduledTaskList;
31
+ eventMessageHandler;
32
+ logger;
33
+ queueName;
34
+ routingPart = 'scheduled-task';
35
+ constructor(queueProvider, scheduledTaskList, eventMessageHandler, logger, queueName) {
36
+ this.queueProvider = queueProvider;
37
+ this.scheduledTaskList = scheduledTaskList;
38
+ this.eventMessageHandler = eventMessageHandler;
39
+ this.logger = logger;
40
+ this.queueName = queueName;
41
+ }
42
+ async onInit() {
43
+ await this.queueProvider.init?.();
44
+ if (!this.queueName) {
45
+ return;
46
+ }
47
+ const config = this.queueProvider.getConfig();
48
+ const eventListeners = Utils.collectEventBusListeners(this.scheduledTaskList);
49
+ await this.subscribe(this.queueName, this.eventMessageHandler.eventListenersMessageHandler.bind(this.eventMessageHandler, eventListeners), {
50
+ listener: config.listenerOptions,
51
+ });
52
+ for (const listener of this.scheduledTaskList) {
53
+ this.logger.info(`Scheduled task [${listener.event}] initialized successfully`);
54
+ }
55
+ }
56
+ subscribe(subscriptionName, messageHandler, options) {
57
+ const routingKey = `${this.queueProvider.getServiceName()}.${this.routingPart}`;
58
+ return this.queueProvider.subscribe(subscriptionName, messageHandler, { ...options, routingKey });
59
+ }
60
+ publish(scheduledTaskName, serviceName) {
61
+ return this.queueProvider.publish(scheduledTaskName, {}, { routingKey: `${serviceName}.${this.routingPart}` });
62
+ }
63
+ }
64
+ exports.ScheduledTask = ScheduledTask;
65
+ //# sourceMappingURL=scheduledTask.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduledTask.js","sourceRoot":"","sources":["../../src/services/scheduledTask.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,gDAAiC;AAIjC,MAAa,aAAa;IAID;IACA;IACA;IAEA;IACA;IARJ,WAAW,GAAW,gBAAgB,CAAA;IAEvD,YACqB,aAA+B,EAC/B,iBAAqC,EACrC,mBAAwC,EAExC,MAAc,EAC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CAAC,iBAAiB,EAAE,EAAE,EAAE,EAAE,UAAU,EAAE,GAAG,WAAW,IAAI,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC,CAAA;IAClH,CAAC;CACJ;AA3CD,sCA2CC"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Task = void 0;
4
+ class Task {
5
+ queueProvider;
6
+ taskList;
7
+ eventMessageHandler;
8
+ logger;
9
+ constructor(queueProvider, taskList, eventMessageHandler, logger) {
10
+ this.queueProvider = queueProvider;
11
+ this.taskList = taskList;
12
+ this.eventMessageHandler = eventMessageHandler;
13
+ this.logger = logger;
14
+ }
15
+ tasksMap = new Map();
16
+ async onInit() {
17
+ await this.queueProvider.init?.();
18
+ if (this.taskList.length === 0) {
19
+ return;
20
+ }
21
+ this.logger.info('Start Tasks listener initialization');
22
+ for (const task of this.taskList) {
23
+ await this.subscribeTask(task);
24
+ this.tasksMap.set(task.name, task);
25
+ }
26
+ }
27
+ subscribe(taskName, messageHandler, options) {
28
+ return this.queueProvider.subscribeTask(this.getTaskQueueName(taskName), messageHandler, options);
29
+ }
30
+ publish(taskName, payload, delay) {
31
+ if (delay) {
32
+ const task = this.tasksMap.get(taskName);
33
+ if (task && !task.isDelayed) {
34
+ throw new Error('Delay option could be used only with delayed tasks');
35
+ }
36
+ }
37
+ const queueName = this.getTaskQueueName(taskName);
38
+ return this.queueProvider.publishTask(queueName, payload, delay);
39
+ }
40
+ async subscribeTask(task) {
41
+ await this.subscribe(task.name, this.eventMessageHandler.eventListenerMessageHandler.bind(this.eventMessageHandler, task), {
42
+ delayed: task.isDelayed || false,
43
+ });
44
+ }
45
+ getTaskQueueName(taskName) {
46
+ return `TasksQueue${this.capitalizeFirstLetter(this.queueProvider.getServiceName())}[${taskName}]`;
47
+ }
48
+ capitalizeFirstLetter(s) {
49
+ return s
50
+ .split('-')
51
+ .map((str) => str.charAt(0).toUpperCase() + str.slice(1))
52
+ .join('');
53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IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;gBACzB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;aACxE;SACJ;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAA;QAEjD,OAAO,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,CAAA;IACpE,CAAC;IAEO,KAAK,CAAC,aAAa,CAAC,IAAkB;QAC1C,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,mBAAmB,CAAC,2BAA2B,CAAC,IAAI,CAAC,IAAI,CAAC,mBAAmB,EAAE,IAAI,CAAC,EAAE;YACvH,OAAO,EAAE,IAAI,CAAC,SAAS,IAAI,KAAK;SACnC,CAAC,CAAA;IACN,CAAC;IAEO,gBAAgB,CAAC,QAAgB;QACrC,OAAO,aAAa,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,aAAa,CAAC,cAAc,EAAE,CAAC,IAAI,QAAQ,GAAG,CAAA;IACtG,CAAC;IAEO,qBAAqB,CAAC,CAAS;QACnC,OAAO,CAAC;aACH,KAAK,CAAC,GAAG,CAAC;aACV,GAAG,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;aAChE,IAAI,CAAC,EAAE,CAAC,CAAA;IACjB,CAAC;CACJ;AA1DD,oBA0DC"}
@@ -0,0 +1,5 @@
1
+ declare const _default: {
2
+ DEFAULT_ROUTING_KEY: string;
3
+ APP_ID: string;
4
+ };
5
+ export default _default;
@@ -0,0 +1,3 @@
1
+ export * from './interfaces';
2
+ export { EventName, QueueName } from './interfaces/queueConfig';
3
+ export * from './services';
@@ -0,0 +1,17 @@
1
+ import { EventBus, EventMessageHandler, EventMessageValidator, ExternalCommunicator, ExternalCommunicatorChannel, ExternalEventBus, Queue, ScheduledTask, Task } from '../services';
2
+ export type CommonQueueDeps = {
3
+ queue: Queue;
4
+ eventMessageHandler: EventMessageHandler;
5
+ eventMessageValidator: EventMessageValidator;
6
+ externalChannel: ExternalCommunicatorChannel;
7
+ };
8
+ export type InternalQueueDeps = {
9
+ eventBus?: EventBus;
10
+ task?: Task;
11
+ scheduledTask?: ScheduledTask;
12
+ };
13
+ export type ExternalQueueDeps = {
14
+ externalEventBus: ExternalEventBus;
15
+ external: ExternalCommunicator;
16
+ };
17
+ export type QueueDeps = CommonQueueDeps & InternalQueueDeps & ExternalQueueDeps;
@@ -0,0 +1,35 @@
1
+ import { HttpStatusCode } from '@diia-inhouse/types';
2
+ import { ValidationSchema } from '@diia-inhouse/validators';
3
+ import { EventName, Topic } from './queueConfig';
4
+ import { EventBusListener } from '.';
5
+ export type EventListeners = Partial<Record<EventName, EventBusListener>>;
6
+ export interface ReceiveOps {
7
+ timeout?: number;
8
+ async?: boolean;
9
+ requestUuid?: string;
10
+ ignoreCache?: boolean;
11
+ retry?: boolean;
12
+ }
13
+ export interface ReceiveDirectOps {
14
+ topic?: Topic;
15
+ validationRules?: ValidationSchema;
16
+ ignoreCache?: boolean;
17
+ retry?: boolean;
18
+ timeout?: number;
19
+ }
20
+ export interface ExternalCommunicatorResponseError {
21
+ http_code: HttpStatusCode;
22
+ message?: string;
23
+ data?: Record<string, unknown>;
24
+ }
25
+ export interface ExternalCommunicatorSuccessResponse<T> {
26
+ uuid: string;
27
+ response: T;
28
+ error: never;
29
+ }
30
+ export interface ExternalCommunicatorFailureResponse {
31
+ uuid: string;
32
+ error?: ExternalCommunicatorResponseError;
33
+ response: never;
34
+ }
35
+ export type ExternalCommunicatorResponse<T> = ExternalCommunicatorSuccessResponse<T> | ExternalCommunicatorFailureResponse;
@@ -0,0 +1,89 @@
1
+ import { Options } from 'amqplib';
2
+ import { ValidationError } from '@diia-inhouse/errors';
3
+ import { LogData } from '@diia-inhouse/types';
4
+ import { ValidationSchema } from '@diia-inhouse/validators';
5
+ import { MessagePayload, QueueMessage, QueueMessageMetaData, RabbitMQConfig } from './providers/rabbitmq';
6
+ import { EventName, QueueConfigByQueueName, QueueName, ServiceConfigByConfigType, ServiceRulesConfig, Topic, TopicConfigByConfigType } from './queueConfig';
7
+ export * from './providers/rabbitmq';
8
+ export * from './deps';
9
+ export { QueueConfigType } from './queueConfig';
10
+ export interface EventBusQueue {
11
+ subscribe(subscriptionName: QueueName, messageHandler: MessageHandler, options?: SubscribeOptions): Promise<boolean>;
12
+ publish(eventName: EventName, message: MessagePayload, options?: PublishInternalEventOptions): Promise<boolean>;
13
+ }
14
+ export interface TaskQueue {
15
+ subscribe(taskName: string, messageHandler: MessageHandler, options?: SubscribeOptions): Promise<boolean>;
16
+ publish(taskName: string, message: MessagePayload, delay?: number): Promise<boolean>;
17
+ }
18
+ export interface ScheduledTasksQueue {
19
+ subscribe(subscriptionName: QueueName, messageHandler: MessageHandler, options?: SubscribeOptions): Promise<boolean>;
20
+ publish(scheduledTaskName: EventName, serviceName: string): Promise<boolean>;
21
+ }
22
+ export interface ExternalEventBusQueue {
23
+ subscribe(messageHandler: MessageHandler, options?: SubscribeOptions): Promise<boolean>;
24
+ publish(eventName: EventName, message: MessagePayload, options?: PublishExternalEventOptions): Promise<boolean>;
25
+ publishDirect<T>(eventName: string, message: MessagePayload, topic?: Topic, options?: PublishDirectOptions): Promise<T>;
26
+ }
27
+ export interface EventBusListener {
28
+ event: EventName;
29
+ /** @deprecated use receive direct mechanism */
30
+ isSync?: boolean;
31
+ validationRules?: ValidationSchema;
32
+ validationErrorHandler?(error: ValidationError, uuid: string): Promise<void>;
33
+ getServiceCode?(payload: unknown): string;
34
+ handler?(payload: unknown, meta: QueueMessageMetaData): Promise<unknown | void>;
35
+ }
36
+ export interface TaskListener {
37
+ name: string;
38
+ isDelayed?: boolean;
39
+ validationRules: ValidationSchema;
40
+ getServiceCode?(payload: unknown): string;
41
+ handler(payload: unknown, meta: QueueMessageMetaData): Promise<void>;
42
+ }
43
+ export declare enum QueueConnectionType {
44
+ Internal = "internal",
45
+ External = "external"
46
+ }
47
+ export type QueueConfig = RabbitMQConfig;
48
+ export interface InternalQueueConfig extends QueueConfig {
49
+ queueName?: string;
50
+ scheduledTaskQueueName?: string;
51
+ }
52
+ export type QueueConnectionConfig = {
53
+ serviceRulesConfig: ServiceRulesConfig;
54
+ [QueueConnectionType.Internal]?: InternalQueueConfig;
55
+ [QueueConnectionType.External]?: QueueConfig;
56
+ };
57
+ export interface QueueContext {
58
+ logData?: LogData;
59
+ }
60
+ export interface ListenerOptions {
61
+ queueOptions?: Options.AssertQueue;
62
+ prefetchCount?: number;
63
+ }
64
+ export interface SubscribeOptions {
65
+ routingKey?: string;
66
+ queueSuffix?: string;
67
+ listener?: ListenerOptions;
68
+ delayed?: boolean;
69
+ }
70
+ export type MessageHandler = (msg: QueueMessage | null) => Promise<void>;
71
+ export interface AggregatedQueueConfigs {
72
+ serviceConfig: ServiceConfigByConfigType;
73
+ queueConfig: QueueConfigByQueueName;
74
+ topicConfig: TopicConfigByConfigType;
75
+ }
76
+ export interface PublishDirectOptions {
77
+ timeout?: number;
78
+ ignoreCache?: boolean;
79
+ retry?: boolean;
80
+ }
81
+ interface PublishOptions {
82
+ publishTimeout?: number;
83
+ throwOnPublishTimeout?: boolean;
84
+ }
85
+ export interface PublishExternalEventOptions extends PublishOptions, PublishDirectOptions {
86
+ }
87
+ export interface PublishInternalEventOptions extends PublishOptions {
88
+ routingKey?: string;
89
+ }
@@ -0,0 +1,7 @@
1
+ export interface TotalListenerChannelErrorsLabelsMap {
2
+ queueName: string;
3
+ }
4
+ export interface TotalMessageHandlerErrorsLabelsMap {
5
+ event: string;
6
+ serviceName: string;
7
+ }