@jetit/publisher 1.3.0 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetit/publisher",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "type": "commonjs",
5
5
  "dependencies": {
6
6
  "@jetit/id": "0.0.11",
@@ -7,6 +7,7 @@ export declare class Streams {
7
7
  private _redisGroups?;
8
8
  private consumerGroupName;
9
9
  private instanceId;
10
+ private instanceUniqueId;
10
11
  private cleanUpTimer;
11
12
  private eventsListened;
12
13
  get redisPublisher(): RedisType;
@@ -148,7 +149,9 @@ export declare class Streams {
148
149
  */
149
150
  close(): Promise<void>;
150
151
  private clearSubscribedEvents;
152
+ private registerSubscribedEvent;
151
153
  private registerConsumerGroup;
152
- releaseAllClaims(eventName: string): Promise<void>;
154
+ private registerConsumerGroupName;
155
+ releaseAllClaims(streamName: string): Promise<void>;
153
156
  private cleanupAcknowledgedMessages;
154
157
  }
@@ -40,12 +40,13 @@ class Streams {
40
40
  * const streams = new Streams('POS');
41
41
  */
42
42
  constructor(serviceName) {
43
- var _a;
43
+ var _a, _b;
44
44
  this.eventsListened = [];
45
- this.instanceId = `${serviceName}:${(0, id_1.generateID)('HEX', 'FE')}`;
45
+ this.instanceUniqueId = (_a = process.env['INSTANCE_ID']) !== null && _a !== void 0 ? _a : (0, id_1.generateID)('HEX', 'FE');
46
+ this.instanceId = `${serviceName}:${this.instanceUniqueId}`;
46
47
  console.log(this.instanceId);
47
48
  this.consumerGroupName = `cg-${serviceName}`;
48
- const cleanUpInterval = (_a = parseInt(process.env['CLEANUP_INTERVAL'] || `${1000 * 60 * 60}`, 10)) !== null && _a !== void 0 ? _a : 1000 * 60 * 60;
49
+ const cleanUpInterval = (_b = parseInt(process.env['CLEANUP_INTERVAL'] || `${1000 * 60 * 60}`, 10)) !== null && _b !== void 0 ? _b : 1000 * 60 * 60;
49
50
  setTimeout(() => this.runClear(cleanUpInterval), 60000);
50
51
  this.cleanUpTimer = setInterval(() => {
51
52
  this.runClear(cleanUpInterval);
@@ -199,6 +200,7 @@ class Streams {
199
200
  */
200
201
  listen(eventName, maxRetries = 5, initialDelay = 1000) {
201
202
  this.registerConsumerGroup(eventName); // Registers the consumer group for listening to the message
203
+ this.registerConsumerGroupName().then();
202
204
  this.eventsListened.push(eventName);
203
205
  return this.listenInternals(eventName).pipe((0, rxjs_1.retry)({
204
206
  count: maxRetries,
@@ -214,7 +216,8 @@ class Streams {
214
216
  }
215
217
  listenInternals(eventName) {
216
218
  try {
217
- this.createConsumerGroup(eventName);
219
+ this.createConsumerGroup(eventName).then();
220
+ this.registerSubscribedEvent(eventName).then();
218
221
  const bs = new rxjs_1.BehaviorSubject(null);
219
222
  const observable = bs.asObservable().pipe((0, rxjs_1.skip)(1));
220
223
  const streamName = `${eventName}:${this.consumerGroupName}`;
@@ -383,36 +386,51 @@ class Streams {
383
386
  let x = this.eventsListened.length;
384
387
  for (const eventName of this.eventsListened) {
385
388
  console.log(`${eventName} is being cleared in publisher`);
389
+ const streamName = `${eventName}:${this.consumerGroupName}`;
386
390
  yield this.redisGroups.srem(`${eventName}`, this.consumerGroupName);
387
391
  console.log(`${eventName} is removed from ${this.consumerGroupName}`);
388
392
  // Releasing all claims based on info from: https://redis.io/commands/xgroup-delconsumer/
389
- yield this.releaseAllClaims(eventName);
393
+ yield this.releaseAllClaims(streamName);
390
394
  console.log(`${eventName} removes all claims`);
391
- yield this.redisGroups.xgroup('DELCONSUMER', eventName, this.consumerGroupName, this.instanceId);
395
+ yield this.redisGroups.xgroup('DELCONSUMER', streamName, this.consumerGroupName, this.instanceId);
392
396
  console.log(`${eventName} is deleted as a consumer from ${this.consumerGroupName}, ${this.instanceId}`);
393
397
  console.log(x--);
394
398
  }
395
399
  });
396
400
  }
401
+ registerSubscribedEvent(eventName) {
402
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
403
+ const key = `instance:${this.instanceId}:subscribedEvents`;
404
+ console.log(`Registering event name for ${this.consumerGroupName} with key : ${key}`);
405
+ yield this.redisGroups.sadd(key, eventName);
406
+ });
407
+ }
397
408
  registerConsumerGroup(eventName) {
398
409
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
399
410
  yield this.redisGroups.sadd(`${eventName}`, this.consumerGroupName);
400
411
  });
401
412
  }
402
- releaseAllClaims(eventName) {
413
+ registerConsumerGroupName() {
414
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
415
+ const key = `instance:${this.instanceUniqueId}:consumerGroupName`;
416
+ console.log(`Registering service name ${this.consumerGroupName} for key : ${key}`);
417
+ yield this.redisGroups.set(key, this.consumerGroupName);
418
+ });
419
+ }
420
+ releaseAllClaims(streamName) {
403
421
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
404
422
  /**
405
423
  * Retrieve the pending messages for the consumer. Note this only fetches the last
406
424
  * 10000 events assigned to this consumer. This function has been modified to make sure
407
425
  * that there is a temp instance that claims all this messages
408
426
  */
409
- const pendingMessages = (yield this.redisGroups.xpending(eventName, this.consumerGroupName, '-', '+', 10000, this.instanceId));
427
+ const pendingMessages = (yield this.redisGroups.xpending(streamName, this.consumerGroupName, '-', '+', 10000, this.instanceId));
410
428
  if (pendingMessages && pendingMessages.length > 0) {
411
429
  console.log(`${pendingMessages.length} messages to clean up.`);
412
430
  const transaction = this.redisGroups.multi({ pipeline: true });
413
431
  const tempConsumerId = `${this.instanceId}-temp`;
414
432
  for (const [messageId] of pendingMessages) {
415
- transaction.xclaim(eventName, this.consumerGroupName, tempConsumerId, 10, messageId);
433
+ transaction.xclaim(streamName, this.consumerGroupName, tempConsumerId, 10, messageId);
416
434
  }
417
435
  yield transaction.exec();
418
436
  }