@codefresh-io/eventbus 3.0.0-alpha.0 → 3.0.0-alpha.2
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/lib/Bus.js +24 -25
- package/lib/Store.js +2 -2
- package/lib/index.js +2 -2
- package/package.json +1 -1
package/lib/Bus.js
CHANGED
|
@@ -42,7 +42,7 @@ class Bus extends EventEmitter {
|
|
|
42
42
|
this._forbidNewConsumers = false;
|
|
43
43
|
this._reSubscribe = this._reSubscribe.bind(this);
|
|
44
44
|
this._reconnect = this._reconnect.bind(this);
|
|
45
|
-
/** @type {Map<amqplib.Channel, string[]>} */
|
|
45
|
+
/** @type {Map<amqplib.Channel, { queueName: string, consumerTag: string }[]>} */
|
|
46
46
|
this.consumerTagsByChannel = new Map();
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -71,16 +71,16 @@ class Bus extends EventEmitter {
|
|
|
71
71
|
for (const ch of this.consumerTagsByChannel.keys()) {
|
|
72
72
|
const consumerTags = this.consumerTagsByChannel.get(ch) ?? [];
|
|
73
73
|
const results = await Promise.allSettled(
|
|
74
|
-
consumerTags.map((
|
|
74
|
+
consumerTags.map((tagInfo) => ch.cancel(tagInfo.consumerTag))
|
|
75
75
|
);
|
|
76
76
|
for (let i = 0; i < results.length; i++) {
|
|
77
77
|
const result = results[i];
|
|
78
|
-
const
|
|
78
|
+
const tagInfo = consumerTags[i];
|
|
79
79
|
if (result.status === 'rejected') {
|
|
80
|
-
this._logger.error(`Failed to cancel consumer with tag "${consumerTag}". Ignoring error`, { error: result.reason });
|
|
80
|
+
this._logger.error(`Failed to cancel consumer with tag "${tagInfo.consumerTag}" on queue "${tagInfo.queueName}". Ignoring error`, { error: result.reason });
|
|
81
81
|
continue;
|
|
82
82
|
}
|
|
83
|
-
this._logger.debug(`Consumer with tag "${consumerTag}" was cancelled successfully`);
|
|
83
|
+
this._logger.debug(`Consumer with tag "${tagInfo.consumerTag}" on queue "${tagInfo.queueName}" was cancelled successfully`);
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
this.consumerTagsByChannel.clear();
|
|
@@ -108,12 +108,12 @@ class Bus extends EventEmitter {
|
|
|
108
108
|
async quit(options = {}) {
|
|
109
109
|
this._terminating = true;
|
|
110
110
|
if (options.force) {
|
|
111
|
-
this._logger.warn('Forcefully terminating
|
|
111
|
+
this._logger.warn('Forcefully terminating bus connection. All consumers in progress will be interrupted and may not finish processing messages');
|
|
112
112
|
await this._closeConnection();
|
|
113
113
|
this._terminated = true;
|
|
114
114
|
return;
|
|
115
115
|
}
|
|
116
|
-
this._logger.info('Gracefully terminating
|
|
116
|
+
this._logger.info('Gracefully terminating bus connection');
|
|
117
117
|
await this.cancelAllConsumers();
|
|
118
118
|
await this.waitForConsumersToFinish();
|
|
119
119
|
await this._closeConnection();
|
|
@@ -121,10 +121,10 @@ class Bus extends EventEmitter {
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
async _closeConnection() {
|
|
124
|
-
this._logger.info('Closing
|
|
124
|
+
this._logger.info('Closing bus connection');
|
|
125
125
|
await this.connection?.close();
|
|
126
126
|
this.subscribers = {};
|
|
127
|
-
this._logger.info('
|
|
127
|
+
this._logger.info('Bus connection is closed');
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
/**
|
|
@@ -134,19 +134,19 @@ class Bus extends EventEmitter {
|
|
|
134
134
|
this.initializing = true;
|
|
135
135
|
this._createErrorHandler();
|
|
136
136
|
|
|
137
|
-
this._logger.debug('
|
|
137
|
+
this._logger.debug('Bus connecting...');
|
|
138
138
|
this.openPromise = amqplib.connect(this.url);
|
|
139
139
|
this.openPromise
|
|
140
140
|
.then((newConnection) => {
|
|
141
|
-
this._logger.debug('
|
|
141
|
+
this._logger.debug('Bus connected');
|
|
142
142
|
if (this.reconnect && this.connection) {
|
|
143
143
|
this.publishChannel = undefined;
|
|
144
|
-
this._logger.debug('
|
|
144
|
+
this._logger.debug('Bus reconnected');
|
|
145
145
|
}
|
|
146
146
|
this.reconnect = false;
|
|
147
147
|
this.connection = newConnection;
|
|
148
148
|
|
|
149
|
-
this._logger.debug('
|
|
149
|
+
this._logger.debug('Bus is ready');
|
|
150
150
|
this.emit('ready');
|
|
151
151
|
|
|
152
152
|
this.connection.on('error', (err) => {
|
|
@@ -154,17 +154,17 @@ class Bus extends EventEmitter {
|
|
|
154
154
|
});
|
|
155
155
|
|
|
156
156
|
this.connection.on('close', () => {
|
|
157
|
-
this._logger.debug('
|
|
157
|
+
this._logger.debug('Bus connection closed');
|
|
158
158
|
this.emit('close');
|
|
159
159
|
this._reconnect();
|
|
160
160
|
});
|
|
161
161
|
|
|
162
162
|
this.connection.on('blocked', () => {
|
|
163
|
-
this._logger.debug('
|
|
163
|
+
this._logger.debug('Bus connection blocked');
|
|
164
164
|
});
|
|
165
165
|
|
|
166
166
|
this.connection.on('unblocked', () => {
|
|
167
|
-
this._logger.debug('
|
|
167
|
+
this._logger.debug('Bus connection unblocked');
|
|
168
168
|
});
|
|
169
169
|
|
|
170
170
|
return this._reSubscribe();
|
|
@@ -179,14 +179,13 @@ class Bus extends EventEmitter {
|
|
|
179
179
|
|
|
180
180
|
_reconnect() {
|
|
181
181
|
if (this.initializing || this._terminating || this._terminated) {
|
|
182
|
-
this._logger.debug('Skipping
|
|
182
|
+
this._logger.debug('Skipping bus reconnect because it is initializing or terminating');
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
this._logger.debug(`
|
|
186
|
+
this._logger.debug(`Bus reconnecting in ${this.reconnectIntervalMs / 1000} seconds...`);
|
|
187
187
|
this.reconnect = true;
|
|
188
188
|
setTimeout(() => {
|
|
189
|
-
this._logger.debug('Eventbus reconnecting...');
|
|
190
189
|
this.init();
|
|
191
190
|
}, this.reconnectIntervalMs);
|
|
192
191
|
}
|
|
@@ -236,12 +235,12 @@ class Bus extends EventEmitter {
|
|
|
236
235
|
* @returns {EventEmitter} An EventEmitter that emits 'error' event when an error occurs in the handler or during subscription process.
|
|
237
236
|
*/
|
|
238
237
|
subscribe(eventName, handler, options = {}, restarting) {
|
|
239
|
-
this._logger.debug(`Subscribing to event "${eventName}", options:`, options);
|
|
238
|
+
this._logger.debug(`Subscribing to event "${eventName}", options:`, { options });
|
|
240
239
|
const subscriberEmitter = new EventEmitter();
|
|
241
240
|
subscriberEmitter.on('error', () => {});
|
|
242
241
|
|
|
243
242
|
if (this._forbidNewConsumers || this._terminating || this._terminated) {
|
|
244
|
-
this._logger.warn(`Skipping new consumer subscription to "${eventName}" because
|
|
243
|
+
this._logger.warn(`Skipping new consumer subscription to "${eventName}" because bus is terminating or terminated or new consumers are forbidden`);
|
|
245
244
|
return subscriberEmitter;
|
|
246
245
|
}
|
|
247
246
|
|
|
@@ -316,8 +315,8 @@ class Bus extends EventEmitter {
|
|
|
316
315
|
ch.ack(msg);
|
|
317
316
|
}
|
|
318
317
|
const content = this._deserialize(msg.content.toString());
|
|
319
|
-
this._consumersInProgressCount
|
|
320
|
-
await handler(content).finally(() => this._consumersInProgressCount
|
|
318
|
+
this._consumersInProgressCount += 1;
|
|
319
|
+
await handler(content).finally(() => this._consumersInProgressCount -= 1);
|
|
321
320
|
} catch (error) {
|
|
322
321
|
subscriberEmitter.emit('error', error);
|
|
323
322
|
if (!finished && waitForAck) {
|
|
@@ -329,7 +328,7 @@ class Bus extends EventEmitter {
|
|
|
329
328
|
}
|
|
330
329
|
});
|
|
331
330
|
const consumerTags = this.consumerTagsByChannel.get(ch) ?? [];
|
|
332
|
-
consumerTags.push(consumerTag);
|
|
331
|
+
consumerTags.push({ queueName, consumerTag });
|
|
333
332
|
this.consumerTagsByChannel.set(ch, consumerTags);
|
|
334
333
|
})
|
|
335
334
|
.catch((err) => {
|
|
@@ -352,7 +351,7 @@ class Bus extends EventEmitter {
|
|
|
352
351
|
*/
|
|
353
352
|
async _getSubscribeChannel() {
|
|
354
353
|
const model = await this.openPromise;
|
|
355
|
-
return model?.createChannel();
|
|
354
|
+
return await model?.createChannel();
|
|
356
355
|
}
|
|
357
356
|
|
|
358
357
|
/**
|
package/lib/Store.js
CHANGED
|
@@ -51,9 +51,9 @@ class Store extends EventEmitter {
|
|
|
51
51
|
this._logger.info(`Closing store for microservice "${this.microServiceName}"`);
|
|
52
52
|
if (this.disablePostgresForEventbus || !this.pool) return;
|
|
53
53
|
|
|
54
|
-
this._logger.debug(`Pool client state before closing: total: ${this.pool.totalCount}, idle: ${this.pool.idleCount},
|
|
54
|
+
this._logger.debug(`Pool client state before closing: total: ${this.pool.totalCount}, idle: ${this.pool.idleCount}, waiting: ${this.pool.waitingCount}`);
|
|
55
55
|
await this.pool.end();
|
|
56
|
-
this._logger.debug(`Pool client state after closing: total: ${this.pool.totalCount}, idle: ${this.pool.idleCount},
|
|
56
|
+
this._logger.debug(`Pool client state after closing: total: ${this.pool.totalCount}, idle: ${this.pool.idleCount}, waiting: ${this.pool.waitingCount}`);
|
|
57
57
|
this._logger.info(`Store closed for microservice "${this.microServiceName}"`);
|
|
58
58
|
}
|
|
59
59
|
|
package/lib/index.js
CHANGED
|
@@ -83,13 +83,13 @@ class EventBus extends EventEmitter {
|
|
|
83
83
|
|
|
84
84
|
// wait for both the bus and the store to send their ready event
|
|
85
85
|
this.bus.on('ready', () => {
|
|
86
|
-
this._logger.info(`Eventbus bus is ready.
|
|
86
|
+
this._logger.info(`Eventbus bus is ready. Registered as: ${this.microServiceName}`);
|
|
87
87
|
this.busReady = true;
|
|
88
88
|
this._emitReady();
|
|
89
89
|
});
|
|
90
90
|
|
|
91
91
|
this.store.on('ready', () => {
|
|
92
|
-
this._logger.info(`Eventbus store is ready.
|
|
92
|
+
this._logger.info(`Eventbus store is ready. Registered as: ${this.microServiceName}`);
|
|
93
93
|
this.storeReady = true;
|
|
94
94
|
this._emitReady();
|
|
95
95
|
});
|