@jetit/publisher 1.3.3 → 1.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetit/publisher",
3
- "version": "1.3.3",
3
+ "version": "1.5.0",
4
4
  "type": "commonjs",
5
5
  "dependencies": {
6
6
  "@jetit/id": "0.0.11",
@@ -8,7 +8,7 @@
8
8
  "rxjs": "^7.0.0"
9
9
  },
10
10
  "peerDependencies": {
11
- "tslib": "^2.3.0"
11
+ "tslib": "2.5.0"
12
12
  },
13
13
  "main": "./src/index.js",
14
14
  "types": "./src/index.d.ts"
@@ -152,6 +152,7 @@ export declare class Streams {
152
152
  private registerSubscribedEvent;
153
153
  private registerConsumerGroup;
154
154
  private registerConsumerGroupName;
155
+ private scanAndClaimAUnclaimedMessage;
155
156
  releaseAllClaims(streamName: string): Promise<void>;
156
157
  private cleanupAcknowledgedMessages;
157
158
  }
@@ -222,6 +222,9 @@ class Streams {
222
222
  const observable = bs.asObservable().pipe((0, rxjs_1.skip)(1));
223
223
  const streamName = `${eventName}:${this.consumerGroupName}`;
224
224
  this.redisSubscriber.subscribe(eventName);
225
+ this.scanAndClaimAUnclaimedMessage(streamName)
226
+ .then()
227
+ .catch((e) => console.log('PUBLISHER: Err in handling unclaimed Messages ' + e.message));
225
228
  const processMessage = () => {
226
229
  try {
227
230
  process.nextTick(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
@@ -247,6 +250,9 @@ class Streams {
247
250
  yield transaction.exec();
248
251
  }
249
252
  }
253
+ this.scanAndClaimAUnclaimedMessage(streamName)
254
+ .then()
255
+ .catch((e) => console.log('PUBLISHER: Err in handling unclaimed Messages ' + e.message));
250
256
  }));
251
257
  }
252
258
  catch (e) {
@@ -315,7 +321,7 @@ class Streams {
315
321
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
316
322
  console.log(`PUBLISHER: Running recoverCrashedConsumerMessages`);
317
323
  const streamName = `${eventName}:${this.consumerGroupName}`;
318
- const pendingMessages = (yield this.redisGroups.xpending(streamName, this.consumerGroupName));
324
+ const pendingMessages = (yield this.redisGroups.xpending(streamName, this.consumerGroupName, 'IDLE', 10000, 0));
319
325
  if (!pendingMessages)
320
326
  return;
321
327
  const [, minId, maxId, consumers] = pendingMessages;
@@ -340,6 +346,10 @@ class Streams {
340
346
  yield transaction.exec().catch(publisherErrorHandler);
341
347
  }
342
348
  }
349
+ // Remove the consumer if it has messages pending for more than 10 secs
350
+ // NOTE: Ideally, this might never happen as messages will automatically be reassigned
351
+ console.log(`PUBLISHER: Closing consumer in ${this.consumerGroupName} with ID: ${consumer}`);
352
+ yield this.redisGroups.xgroup('DELCONSUMER', streamName, this.consumerGroupName, consumer);
343
353
  }
344
354
  });
345
355
  }
@@ -417,6 +427,16 @@ class Streams {
417
427
  yield this.redisGroups.set(key, this.consumerGroupName);
418
428
  });
419
429
  }
430
+ scanAndClaimAUnclaimedMessage(streamName) {
431
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
432
+ const rows = yield this.redisGroups.xautoclaim(streamName, this.consumerGroupName, this.instanceId, 500, 0, 'COUNT', 1);
433
+ if (rows) {
434
+ yield this.redisPublisher.publish(streamName.split(':')[0], '');
435
+ return this.scanAndClaimAUnclaimedMessage(streamName);
436
+ }
437
+ return;
438
+ });
439
+ }
420
440
  releaseAllClaims(streamName) {
421
441
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
422
442
  /**
@@ -428,7 +448,7 @@ class Streams {
428
448
  if (pendingMessages && pendingMessages.length > 0) {
429
449
  console.log(`${pendingMessages.length} messages to clean up.`);
430
450
  const transaction = this.redisGroups.multi({ pipeline: true });
431
- const tempConsumerId = `${this.instanceId}-temp`;
451
+ const tempConsumerId = `${this.consumerGroupName}-temp`;
432
452
  for (const [messageId] of pendingMessages) {
433
453
  transaction.xclaim(streamName, this.consumerGroupName, tempConsumerId, 10, messageId);
434
454
  }