@autofleet/rabbit 3.4.0-beta.18 → 3.4.0-beta.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/dist/index.js CHANGED
@@ -168,14 +168,14 @@ class RabbitMq {
168
168
  this.publishChannelSetupPromise = null;
169
169
  this.connection = null;
170
170
  this.publishConnection = {
171
- amqpConnection: null,
171
+ connection: null,
172
172
  creatingConnection: false,
173
173
  connectionCreatedEventName: 'publishConnectionCreated',
174
174
  connectionFailedEventName: 'publishConnectionFailed',
175
175
  blockReconnect: false,
176
176
  };
177
177
  this.consumeConnection = {
178
- amqpConnection: null,
178
+ connection: null,
179
179
  creatingConnection: false,
180
180
  connectionCreatedEventName: 'consumeConnectionCreated',
181
181
  connectionFailedEventName: 'consumeConnectionFailed',
@@ -207,17 +207,17 @@ class RabbitMq {
207
207
  this.oldPublishChannel = null;
208
208
  this.oldPublishChannelSetupPromise = null;
209
209
  this.oldPublishConnection = {
210
- amqpConnection: null,
210
+ connection: null,
211
211
  creatingConnection: false,
212
- connectionCreatedEventName: 'oldPublishConnectionCreated',
213
- connectionFailedEventName: 'oldPublishConnectionFailed',
212
+ connectionCreatedEventName: 'publishConnectionCreated',
213
+ connectionFailedEventName: 'publishConnectionFailed',
214
214
  blockReconnect: false,
215
215
  };
216
216
  this.oldConsumeConnection = {
217
- amqpConnection: null,
217
+ connection: null,
218
218
  creatingConnection: false,
219
- connectionCreatedEventName: 'oldConsumeConnectionCreated',
220
- connectionFailedEventName: 'oldConsumeConnectionFailed',
219
+ connectionCreatedEventName: 'consumeConnectionCreated',
220
+ connectionFailedEventName: 'consumeConnectionFailed',
221
221
  blockReconnect: false,
222
222
  };
223
223
  this.oldCreatingConnection = false;
@@ -230,7 +230,7 @@ class RabbitMq {
230
230
  }
231
231
  async getConnection(connection) {
232
232
  return new Promise(async (resolve, reject) => {
233
- const { amqpConnection: connectionLocal, creatingConnection, connectionCreatedEventName, connectionFailedEventName, blockReconnect, } = connection;
233
+ const { connection: connectionLocal, creatingConnection, connectionCreatedEventName, connectionFailedEventName, blockReconnect, } = connection;
234
234
  if (blockReconnect) {
235
235
  debug('rabbit: block reconnect');
236
236
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -268,7 +268,13 @@ class RabbitMq {
268
268
  const newConnection = await (0, amqp_connection_manager_1.connect)(defaultUrls, {
269
269
  findServers,
270
270
  });
271
- connection.amqpConnection = newConnection;
271
+ connection.connection = newConnection;
272
+ if (connection === this.publishConnection) {
273
+ debug('rabbit: publishConnection set');
274
+ }
275
+ else if (connection === this.consumeConnection) {
276
+ debug('rabbit: consumeConnection set');
277
+ }
272
278
  newConnection.on('error', (err) => {
273
279
  logger_1.default.error('rabbit: connection error', { err });
274
280
  if (!isResolved) {
@@ -301,7 +307,7 @@ class RabbitMq {
301
307
  newConnection.once('connect', async () => {
302
308
  debug('rabbit: connection established');
303
309
  connection.creatingConnection = false;
304
- this.em.emit(connectionCreatedEventName, newConnection);
310
+ this.em.emit(connectionCreatedEventName, connection);
305
311
  isResolved = true;
306
312
  resolve(newConnection);
307
313
  });
@@ -316,7 +322,7 @@ class RabbitMq {
316
322
  logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
317
323
  throw e;
318
324
  }
319
- const channel = localConnection?.createChannel({ ...options });
325
+ const channel = localConnection.createChannel({ ...options });
320
326
  (0, events_1.once)(channel, 'close').then((args) => {
321
327
  logger_1.default.error(`rabbit: channel ${name} closed`);
322
328
  onClose?.(args);
@@ -374,7 +380,7 @@ class RabbitMq {
374
380
  if (!channel) {
375
381
  throw new rabbitError_1.default('channel is not defined');
376
382
  }
377
- debug('rabbit: getting queue length', { queue, connected: this.oldPublishConnection.amqpConnection?.isConnected() });
383
+ debug('rabbit: getting queue length', { queue, connected: this.connection?.isConnected() });
378
384
  return channel?.checkQueue(queue);
379
385
  }
380
386
  async deleteQueue(queue, connection) {
@@ -669,26 +675,26 @@ class RabbitMq {
669
675
  }
670
676
  // TODO: [QUORUM-PHASE-2] After changing the publish from old to new change from the old to the new implementation
671
677
  async isConnected() {
672
- debug('rabbit: start old is connected');
673
- const oldConsumeConnection = await this.getConnectionOld(this.oldConsumeConnection);
674
- const oldPublishConnection = await this.getConnectionOld(this.oldPublishConnection);
675
- const oldIsConnected = oldConsumeConnection.isConnected() && oldPublishConnection.isConnected();
676
- if (!oldIsConnected) {
677
- logger_1.default.error('rabbit: old isConnected - false');
678
+ debug('rabbit: start is connected');
679
+ const consumeConnection = await this.getConnectionOld(this.oldConsumeConnection);
680
+ const publishConnection = await this.getConnectionOld(this.oldPublishConnection);
681
+ const isConnected = consumeConnection.isConnected() && publishConnection.isConnected();
682
+ if (!isConnected) {
683
+ logger_1.default.error('rabbit: isConnected - false');
678
684
  return false;
679
685
  }
680
- const oldChannel = await this.assertChannelOld({ connection: this.oldPublishConnection });
686
+ const channel = await this.assertChannelOld({ connection: this.oldPublishConnection });
681
687
  try {
682
688
  await Promise.all([
683
- oldChannel.waitForConnect(),
684
- ...this.oldConsumers.map((c) => oldChannel.checkQueue(c.queue)),
689
+ channel.waitForConnect(),
690
+ ...this.oldConsumers.map((c) => channel.checkQueue(c.queue)),
685
691
  ]);
686
692
  }
687
693
  catch (e) {
688
- logger_1.default.error('rabbit: old isConnected - false');
694
+ logger_1.default.error('rabbit: isConnected - false');
689
695
  return false;
690
696
  }
691
- logger_1.default.debug('rabbit: old isConnected - true');
697
+ logger_1.default.debug('rabbit: isConnected - true');
692
698
  return true;
693
699
  }
694
700
  async gracefulShutdown(signal) {
@@ -809,10 +815,8 @@ class RabbitMq {
809
815
  logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
810
816
  throw e;
811
817
  }
812
- if (!localConnection) {
813
- throw new Error(`rabbit: couldnt get connection for new channel ${name}`);
814
- }
815
- const channel = localConnection?.createChannel({ ...options });
818
+ debug('rabbit: getNewChannelOld', { localConnection });
819
+ const channel = localConnection.createChannel({ ...options });
816
820
  (0, events_1.once)(channel, 'close').then((args) => {
817
821
  logger_1.default.error(`rabbit: channel ${name} closed`);
818
822
  onClose?.(args);
@@ -829,7 +833,13 @@ class RabbitMq {
829
833
  }
830
834
  async getConnectionOld(connection) {
831
835
  return new Promise(async (resolve, reject) => {
832
- const { amqpConnection: connectionLocal, creatingConnection, connectionCreatedEventName, connectionFailedEventName, blockReconnect, } = connection;
836
+ const { connection: connectionLocal, creatingConnection, connectionCreatedEventName, connectionFailedEventName, blockReconnect, } = connection;
837
+ if (connection === this.oldPublishConnection) {
838
+ debug('rabbit: getConnectionOld publish', { connectionLocal, creatingConnection, blockReconnect });
839
+ }
840
+ else if (connection === this.oldConsumeConnection) {
841
+ debug('rabbit: getConnectionOld consume', { connectionLocal, creatingConnection, blockReconnect });
842
+ }
833
843
  if (blockReconnect) {
834
844
  debug('rabbit: block reconnect');
835
845
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -867,7 +877,13 @@ class RabbitMq {
867
877
  const newConnection = await (0, amqp_connection_manager_1.connect)(defaultUrls, {
868
878
  findServers,
869
879
  });
870
- connection.amqpConnection = newConnection;
880
+ connection.connection = newConnection;
881
+ if (connection === this.oldPublishConnection) {
882
+ debug('rabbit: oldPublishConnection set');
883
+ }
884
+ else if (connection === this.oldConsumeConnection) {
885
+ debug('rabbit: oldConsumeConnection set');
886
+ }
871
887
  newConnection.on('error', (err) => {
872
888
  logger_1.default.error('rabbit: connection error', { err });
873
889
  if (!isResolved) {
@@ -899,7 +915,7 @@ class RabbitMq {
899
915
  newConnection.once('connect', async () => {
900
916
  debug('rabbit: connection established');
901
917
  connection.creatingConnection = false;
902
- this.oldEm.emit(connectionCreatedEventName, newConnection);
918
+ this.oldEm.emit(connectionCreatedEventName, connection);
903
919
  isResolved = true;
904
920
  resolve(newConnection);
905
921
  });
@@ -978,6 +994,7 @@ class RabbitMq {
978
994
  logger_1.default.error('rabbit: channel error', { err });
979
995
  });
980
996
  if (this.oldPublishConnection === connection) {
997
+ debug('rabbit: a new channel assigned to the oldPublishChannel', { connection });
981
998
  this.oldPublishChannel = channel;
982
999
  }
983
1000
  resolve(channel);
@@ -44,7 +44,7 @@ export type AfConsumer = {
44
44
  };
45
45
  export declare const CONSUMER_DEFAULT_OPTIONS: Options.Consume;
46
46
  export type ConnectionData = {
47
- amqpConnection: AmqpConnectionManager | null;
47
+ connection: AmqpConnectionManager | null;
48
48
  creatingConnection: boolean;
49
49
  connectionCreatedEventName: string;
50
50
  connectionFailedEventName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.4.0-beta.18",
3
+ "version": "3.4.0-beta.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
package/src/index.ts CHANGED
@@ -215,14 +215,14 @@ class RabbitMq implements IAfRabbitMq {
215
215
  this.publishChannelSetupPromise = null;
216
216
  this.connection = null;
217
217
  this.publishConnection = {
218
- amqpConnection: null,
218
+ connection: null,
219
219
  creatingConnection: false,
220
220
  connectionCreatedEventName: 'publishConnectionCreated',
221
221
  connectionFailedEventName: 'publishConnectionFailed',
222
222
  blockReconnect: false,
223
223
  };
224
224
  this.consumeConnection = {
225
- amqpConnection: null,
225
+ connection: null,
226
226
  creatingConnection: false,
227
227
  connectionCreatedEventName: 'consumeConnectionCreated',
228
228
  connectionFailedEventName: 'consumeConnectionFailed',
@@ -256,17 +256,17 @@ class RabbitMq implements IAfRabbitMq {
256
256
  this.oldPublishChannel = null;
257
257
  this.oldPublishChannelSetupPromise = null;
258
258
  this.oldPublishConnection = {
259
- amqpConnection: null,
259
+ connection: null,
260
260
  creatingConnection: false,
261
- connectionCreatedEventName: 'oldPublishConnectionCreated',
262
- connectionFailedEventName: 'oldPublishConnectionFailed',
261
+ connectionCreatedEventName: 'publishConnectionCreated',
262
+ connectionFailedEventName: 'publishConnectionFailed',
263
263
  blockReconnect: false,
264
264
  };
265
265
  this.oldConsumeConnection = {
266
- amqpConnection: null,
266
+ connection: null,
267
267
  creatingConnection: false,
268
- connectionCreatedEventName: 'oldConsumeConnectionCreated',
269
- connectionFailedEventName: 'oldConsumeConnectionFailed',
268
+ connectionCreatedEventName: 'consumeConnectionCreated',
269
+ connectionFailedEventName: 'consumeConnectionFailed',
270
270
  blockReconnect: false,
271
271
  };
272
272
  this.oldCreatingConnection = false;
@@ -409,13 +409,12 @@ class RabbitMq implements IAfRabbitMq {
409
409
  async getConnection(connection: ConnectionData) {
410
410
  return new Promise<AmqpConnectionManager>(async (resolve, reject) => {
411
411
  const {
412
- amqpConnection: connectionLocal,
412
+ connection: connectionLocal,
413
413
  creatingConnection,
414
414
  connectionCreatedEventName,
415
415
  connectionFailedEventName,
416
416
  blockReconnect,
417
417
  } = connection;
418
-
419
418
  if (blockReconnect) {
420
419
  debug('rabbit: block reconnect');
421
420
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -457,7 +456,12 @@ class RabbitMq implements IAfRabbitMq {
457
456
  findServers,
458
457
  });
459
458
 
460
- connection.amqpConnection = newConnection;
459
+ connection.connection = newConnection;
460
+ if (connection === this.publishConnection) {
461
+ debug('rabbit: publishConnection set');
462
+ } else if (connection === this.consumeConnection) {
463
+ debug('rabbit: consumeConnection set');
464
+ }
461
465
  newConnection.on('error', (err) => {
462
466
  logger.error('rabbit: connection error', { err });
463
467
  if (!isResolved) {
@@ -492,7 +496,7 @@ class RabbitMq implements IAfRabbitMq {
492
496
  newConnection.once('connect', async () => {
493
497
  debug('rabbit: connection established');
494
498
  connection.creatingConnection = false;
495
- this.em.emit(connectionCreatedEventName, newConnection);
499
+ this.em.emit(connectionCreatedEventName, connection);
496
500
  isResolved = true;
497
501
  resolve(newConnection);
498
502
  });
@@ -509,7 +513,7 @@ class RabbitMq implements IAfRabbitMq {
509
513
  logger.error(`rabbit: error on get connection for new channel ${name} `, { e });
510
514
  throw e;
511
515
  }
512
- const channel = localConnection?.createChannel({ ...options });
516
+ const channel = localConnection.createChannel({ ...options });
513
517
  once(channel, 'close').then((args) => {
514
518
  logger.error(`rabbit: channel ${name} closed`);
515
519
  onClose?.(args);
@@ -572,7 +576,7 @@ class RabbitMq implements IAfRabbitMq {
572
576
  if (!channel) {
573
577
  throw new RabbitError('channel is not defined');
574
578
  }
575
- debug('rabbit: getting queue length', { queue, connected: this.oldPublishConnection.amqpConnection?.isConnected() });
579
+ debug('rabbit: getting queue length', { queue, connected: this.connection?.isConnected() });
576
580
  return channel?.checkQueue(queue);
577
581
  }
578
582
 
@@ -850,6 +854,7 @@ class RabbitMq implements IAfRabbitMq {
850
854
  ]);
851
855
  });
852
856
  }
857
+
853
858
  // TODO: [QUORUM-PHASE-3] Delete the old implementation
854
859
  await this.saveConsumerOld(queue, callback, options);
855
860
  const channelOld: ChannelWrapper = await this.getNewChannelOld({
@@ -922,26 +927,26 @@ class RabbitMq implements IAfRabbitMq {
922
927
  }
923
928
 
924
929
  // TODO: [QUORUM-PHASE-2] After changing the publish from old to new change from the old to the new implementation
925
- async isConnected(): Promise<boolean> {
926
- debug('rabbit: start old is connected');
927
- const oldConsumeConnection = await this.getConnectionOld(this.oldConsumeConnection);
928
- const oldPublishConnection = await this.getConnectionOld(this.oldPublishConnection);
929
- const oldIsConnected = oldConsumeConnection.isConnected() && oldPublishConnection.isConnected();
930
- if (!oldIsConnected) {
931
- logger.error('rabbit: old isConnected - false');
930
+ async isConnected() : Promise<boolean> {
931
+ debug('rabbit: start is connected');
932
+ const consumeConnection = await this.getConnectionOld(this.oldConsumeConnection);
933
+ const publishConnection = await this.getConnectionOld(this.oldPublishConnection);
934
+ const isConnected = consumeConnection.isConnected() && publishConnection.isConnected();
935
+ if (!isConnected) {
936
+ logger.error('rabbit: isConnected - false');
932
937
  return false;
933
938
  }
934
- const oldChannel: any = await this.assertChannelOld({ connection: this.oldPublishConnection });
939
+ const channel: any = await this.assertChannelOld({ connection: this.oldPublishConnection });
935
940
  try {
936
941
  await Promise.all([
937
- oldChannel.waitForConnect(),
938
- ...this.oldConsumers.map((c: AfConsumer) => oldChannel.checkQueue(c.queue)),
942
+ channel.waitForConnect(),
943
+ ...this.oldConsumers.map((c: AfConsumer) => channel.checkQueue(c.queue)),
939
944
  ]);
940
945
  } catch (e) {
941
- logger.error('rabbit: old isConnected - false');
946
+ logger.error('rabbit: isConnected - false');
942
947
  return false;
943
948
  }
944
- logger.debug('rabbit: old isConnected - true');
949
+ logger.debug('rabbit: isConnected - true');
945
950
  return true;
946
951
  }
947
952
 
@@ -1078,12 +1083,8 @@ class RabbitMq implements IAfRabbitMq {
1078
1083
  logger.error(`rabbit: error on get connection for new channel ${name} `, { e });
1079
1084
  throw e;
1080
1085
  }
1081
-
1082
- if (!localConnection) {
1083
- throw new Error(`rabbit: couldnt get connection for new channel ${name}`);
1084
- }
1085
-
1086
- const channel = localConnection?.createChannel({ ...options });
1086
+ debug('rabbit: getNewChannelOld', { localConnection });
1087
+ const channel = localConnection.createChannel({ ...options });
1087
1088
  once(channel, 'close').then((args) => {
1088
1089
  logger.error(`rabbit: channel ${name} closed`);
1089
1090
  onClose?.(args);
@@ -1101,13 +1102,18 @@ class RabbitMq implements IAfRabbitMq {
1101
1102
  async getConnectionOld(connection: ConnectionData) {
1102
1103
  return new Promise<AmqpConnectionManager>(async (resolve, reject) => {
1103
1104
  const {
1104
- amqpConnection: connectionLocal,
1105
+ connection: connectionLocal,
1105
1106
  creatingConnection,
1106
1107
  connectionCreatedEventName,
1107
1108
  connectionFailedEventName,
1108
1109
  blockReconnect,
1109
1110
  } = connection;
1110
1111
 
1112
+ if (connection === this.oldPublishConnection) {
1113
+ debug('rabbit: getConnectionOld publish', { connectionLocal, creatingConnection, blockReconnect });
1114
+ } else if (connection === this.oldConsumeConnection) {
1115
+ debug('rabbit: getConnectionOld consume', { connectionLocal, creatingConnection, blockReconnect });
1116
+ }
1111
1117
  if (blockReconnect) {
1112
1118
  debug('rabbit: block reconnect');
1113
1119
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -1150,7 +1156,12 @@ class RabbitMq implements IAfRabbitMq {
1150
1156
  findServers,
1151
1157
  });
1152
1158
 
1153
- connection.amqpConnection = newConnection;
1159
+ connection.connection = newConnection;
1160
+ if (connection === this.oldPublishConnection) {
1161
+ debug('rabbit: oldPublishConnection set');
1162
+ } else if (connection === this.oldConsumeConnection) {
1163
+ debug('rabbit: oldConsumeConnection set');
1164
+ }
1154
1165
  newConnection.on('error', (err) => {
1155
1166
  logger.error('rabbit: connection error', { err });
1156
1167
  if (!isResolved) {
@@ -1184,7 +1195,7 @@ class RabbitMq implements IAfRabbitMq {
1184
1195
  newConnection.once('connect', async () => {
1185
1196
  debug('rabbit: connection established');
1186
1197
  connection.creatingConnection = false;
1187
- this.oldEm.emit(connectionCreatedEventName, newConnection);
1198
+ this.oldEm.emit(connectionCreatedEventName, connection);
1188
1199
  isResolved = true;
1189
1200
  resolve(newConnection);
1190
1201
  });
@@ -1270,6 +1281,7 @@ class RabbitMq implements IAfRabbitMq {
1270
1281
  logger.error('rabbit: channel error', { err });
1271
1282
  });
1272
1283
  if (this.oldPublishConnection === connection) {
1284
+ debug('rabbit: a new channel assigned to the oldPublishChannel', { connection });
1273
1285
  this.oldPublishChannel = channel;
1274
1286
  }
1275
1287
  resolve(channel);
package/src/lib/types.ts CHANGED
@@ -64,7 +64,7 @@ export const CONSUMER_DEFAULT_OPTIONS: Options.Consume = {
64
64
  };
65
65
 
66
66
  export type ConnectionData = {
67
- amqpConnection: AmqpConnectionManager | null;
67
+ connection: AmqpConnectionManager | null;
68
68
  creatingConnection: boolean;
69
69
  connectionCreatedEventName: string;
70
70
  connectionFailedEventName: string;