@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 +1 -1
- package/src/lib/redis/streams.js +35 -33
package/package.json
CHANGED
package/src/lib/redis/streams.js
CHANGED
|
@@ -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),
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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
|
}
|