@jetit/publisher 1.1.2 → 1.1.3

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.1.2",
3
+ "version": "1.1.3",
4
4
  "type": "commonjs",
5
5
  "dependencies": {
6
6
  "@jetit/id": "0.0.11",
@@ -149,5 +149,6 @@ export declare class Streams {
149
149
  close(): Promise<void>;
150
150
  private clearSubscribedEvents;
151
151
  private registerConsumerGroup;
152
+ releaseAllClaims(eventName: string): Promise<void>;
152
153
  private cleanupAcknowledgedMessages;
153
154
  }
@@ -278,16 +278,16 @@ class Streams {
278
278
  if (!streamMessages)
279
279
  return;
280
280
  console.log(`Unprocessed events: ${streamMessages.length}`);
281
- const transaction = this.redisGroups.multi({ pipeline: true });
282
281
  for (const [id, data] of streamMessages) {
283
282
  const eventData = JSON.parse(data[1]);
283
+ const transaction = this.redisGroups.multi();
284
284
  // Republishing the events
285
285
  transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
286
286
  transaction.publish(eventName, '');
287
287
  transaction.xack(streamName, this.consumerGroupName, id);
288
+ yield transaction.exec().catch(publisherErrorHandler);
288
289
  console.log(`Event ${eventName} with ID: ${id} published`);
289
290
  }
290
- yield transaction.exec();
291
291
  }
292
292
  });
293
293
  }
@@ -314,7 +314,6 @@ class Streams {
314
314
  const [, minId, maxId, consumers] = pendingMessages;
315
315
  if (!consumers || consumers.length === 0)
316
316
  return;
317
- const transaction = this.redisGroups.multi({ pipeline: true });
318
317
  for (const [consumer, pendingCount] of consumers) {
319
318
  if (parseInt(pendingCount) > 0) {
320
319
  const pending = (yield this.redisGroups.xpending(streamName, this.consumerGroupName, minId, maxId, Number(pendingCount), consumer));
@@ -323,14 +322,15 @@ class Streams {
323
322
  if (claimedMessage) {
324
323
  const [, data] = claimedMessage[0];
325
324
  const eventData = JSON.parse(data[1]);
325
+ const transaction = this.redisGroups.multi();
326
326
  transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
327
327
  transaction.publish(eventName, '');
328
328
  transaction.xack(streamName, this.consumerGroupName, messageId);
329
+ yield transaction.exec().catch(publisherErrorHandler);
329
330
  }
330
331
  }
331
332
  }
332
333
  }
333
- yield transaction.exec();
334
334
  });
335
335
  }
336
336
  /**
@@ -375,6 +375,9 @@ class Streams {
375
375
  console.log(`${this.eventsListened.length} events to be cleared`);
376
376
  for (const eventName of this.eventsListened) {
377
377
  yield this.redisGroups.srem(`${eventName}`, this.consumerGroupName);
378
+ // Releasing all claims based on info from: https://redis.io/commands/xgroup-delconsumer/
379
+ yield this.releaseAllClaims(eventName);
380
+ yield this.redisGroups.xgroup('DELCONSUMER', eventName, this.consumerGroupName, this.instanceId);
378
381
  }
379
382
  });
380
383
  }
@@ -383,13 +386,28 @@ class Streams {
383
386
  yield this.redisGroups.sadd(`${eventName}`, this.consumerGroupName);
384
387
  });
385
388
  }
389
+ releaseAllClaims(eventName) {
390
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
391
+ // Retrieve the pending messages for the consumer
392
+ const pendingMessages = (yield this.redisGroups.xpending(eventName, this.consumerGroupName, '-', '+', 1000, this.instanceId));
393
+ if (pendingMessages && pendingMessages.length > 0) {
394
+ const transaction = this.redisGroups.multi({ pipeline: true });
395
+ for (const [messageId] of pendingMessages) {
396
+ // Setting the idle time to 10000 ms to make sure this consumer shuts down completely
397
+ const idleTime = 10000;
398
+ transaction.xclaim(eventName, this.consumerGroupName, this.instanceId, idleTime);
399
+ }
400
+ yield transaction.exec();
401
+ }
402
+ });
403
+ }
386
404
  cleanupAcknowledgedMessages(eventName, interval = 60 * 60 * 1000) {
387
405
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
388
406
  const streamName = `${eventName}:${this.consumerGroupName}`;
389
407
  const cleanupThreshold = Date.now() - interval;
390
408
  const acknowledgedMessages = yield this.redisGroups.zrangebyscore(`ack:${streamName}`, '-inf', cleanupThreshold);
391
409
  if (acknowledgedMessages && acknowledgedMessages.length > 0) {
392
- const transaction = this.redisGroups.multi({ pipeline: true });
410
+ const transaction = this.redisGroups.pipeline();
393
411
  // Remove acknowledged messages from the stream
394
412
  for (const messageId of acknowledgedMessages) {
395
413
  transaction.xdel(streamName, messageId);