@jetit/publisher 1.2.0 → 1.3.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.2.0",
3
+ "version": "1.3.0",
4
4
  "type": "commonjs",
5
5
  "dependencies": {
6
6
  "@jetit/id": "0.0.11",
@@ -46,7 +46,7 @@ class Streams {
46
46
  console.log(this.instanceId);
47
47
  this.consumerGroupName = `cg-${serviceName}`;
48
48
  const cleanUpInterval = (_a = parseInt(process.env['CLEANUP_INTERVAL'] || `${1000 * 60 * 60}`, 10)) !== null && _a !== void 0 ? _a : 1000 * 60 * 60;
49
- setTimeout(() => this.runClear(cleanUpInterval), 3000);
49
+ setTimeout(() => this.runClear(cleanUpInterval), 60000);
50
50
  this.cleanUpTimer = setInterval(() => {
51
51
  this.runClear(cleanUpInterval);
52
52
  }, cleanUpInterval);
@@ -272,24 +272,25 @@ class Streams {
272
272
  */
273
273
  republishUnprocessedEvents(eventName) {
274
274
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
275
+ console.log('Republishing Unprocessed Events.');
275
276
  const streamName = `${eventName}:${this.consumerGroupName}`;
276
277
  const result = yield this.redisGroups.xreadgroup('GROUP', this.consumerGroupName, this.instanceId, 'STREAMS', streamName, '>');
277
- if (result) {
278
- const [, streamMessages] = result[0];
279
- if (!streamMessages)
280
- return;
281
- console.log(`Unprocessed events: ${streamMessages.length}`);
282
- for (const [id, data] of streamMessages) {
283
- const transaction = this.redisGroups.multi({ pipeline: true });
284
- const eventData = JSON.parse(data[1]);
285
- // Republishing the events
286
- transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
287
- transaction.publish(eventName, '');
288
- transaction.xack(streamName, this.consumerGroupName, id);
289
- yield transaction.exec().catch(publisherErrorHandler);
290
- console.log(`Event ${eventName} with ID: ${id} published`);
291
- yield transaction.exec();
292
- }
278
+ if (!result)
279
+ return;
280
+ const [, streamMessages] = result[0];
281
+ if (!streamMessages)
282
+ return;
283
+ console.log(`Unprocessed events: ${streamMessages.length}`);
284
+ for (const [id, data] of streamMessages) {
285
+ const transaction = this.redisGroups.multi({ pipeline: true });
286
+ const eventData = JSON.parse(data[1]);
287
+ // Republishing the events
288
+ transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
289
+ transaction.publish(eventName, '');
290
+ transaction.xack(streamName, this.consumerGroupName, id);
291
+ yield transaction.exec().catch(publisherErrorHandler);
292
+ console.log(`Event ${eventName} with ID: ${id} published`);
293
+ yield transaction.exec();
293
294
  }
294
295
  });
295
296
  }
@@ -309,6 +310,7 @@ class Streams {
309
310
  */
310
311
  recoverCrashedConsumerMessages(eventName, idleTimeout = 30000) {
311
312
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
313
+ console.log(`PUBLISHER: Running recoverCrashedConsumerMessages`);
312
314
  const streamName = `${eventName}:${this.consumerGroupName}`;
313
315
  const pendingMessages = (yield this.redisGroups.xpending(streamName, this.consumerGroupName));
314
316
  if (!pendingMessages)
@@ -317,24 +319,24 @@ class Streams {
317
319
  if (!consumers || consumers.length === 0)
318
320
  return;
319
321
  for (const [consumer, pendingCount] of consumers) {
320
- const transaction = this.redisGroups.multi({ pipeline: true });
321
- if (parseInt(pendingCount) > 0) {
322
- const pending = (yield this.redisGroups.xpending(streamName, this.consumerGroupName, minId, maxId, Number(pendingCount), consumer));
323
- for (const [messageId] of pending) {
324
- const claimedMessage = (yield this.redisGroups.xclaim(streamName, this.consumerGroupName, this.instanceId, idleTimeout, messageId));
325
- if (claimedMessage) {
326
- console.log({ claimedMessage: JSON.stringify(claimedMessage) });
327
- const [, data] = claimedMessage[0];
328
- const eventData = JSON.parse(data[1]);
329
- const transaction = this.redisGroups.multi();
330
- transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
331
- transaction.publish(eventName, '');
332
- transaction.xack(streamName, this.consumerGroupName, messageId);
333
- yield transaction.exec().catch(publisherErrorHandler);
334
- }
322
+ if (parseInt(pendingCount) < 0)
323
+ return;
324
+ const pending = (yield this.redisGroups.xpending(streamName, this.consumerGroupName, minId, maxId, Number(pendingCount), consumer));
325
+ if (!pending)
326
+ return;
327
+ for (const [messageId] of pending) {
328
+ const claimedMessage = (yield this.redisGroups.xclaim(streamName, this.consumerGroupName, this.instanceId, idleTimeout, messageId));
329
+ if (claimedMessage) {
330
+ console.log({ claimedMessage: JSON.stringify(claimedMessage) });
331
+ const [, data] = claimedMessage[0];
332
+ const eventData = JSON.parse(data[1]);
333
+ const transaction = this.redisGroups.multi();
334
+ transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
335
+ transaction.publish(eventName, '');
336
+ transaction.xack(streamName, this.consumerGroupName, messageId);
337
+ yield transaction.exec().catch(publisherErrorHandler);
335
338
  }
336
339
  }
337
- yield transaction.exec();
338
340
  }
339
341
  });
340
342
  }