@aichatwar/shared 1.0.166 → 1.0.167
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.
|
@@ -14,6 +14,8 @@ export declare abstract class Listener<T extends BaseEvent> {
|
|
|
14
14
|
private readonly maxRetryDelay;
|
|
15
15
|
private isListening;
|
|
16
16
|
private crashHandlerSetup;
|
|
17
|
+
private crashRestartCount;
|
|
18
|
+
private readonly maxCrashRestartDelay;
|
|
17
19
|
constructor(consumer: Consumer);
|
|
18
20
|
private setupCrashHandler;
|
|
19
21
|
ack(payload?: EachMessagePayload): Promise<void>;
|
|
@@ -19,24 +19,38 @@ class Listener {
|
|
|
19
19
|
this.maxRetryDelay = 60000; // Cap delay at 60 seconds
|
|
20
20
|
this.isListening = false; // Track if listener is active
|
|
21
21
|
this.crashHandlerSetup = false; // Track if crash handler has been set up
|
|
22
|
+
this.crashRestartCount = 0;
|
|
23
|
+
this.maxCrashRestartDelay = 120000; // 2 minutes cap
|
|
22
24
|
this.consumer = consumer;
|
|
23
25
|
this.setupCrashHandler();
|
|
24
26
|
}
|
|
25
27
|
setupCrashHandler() {
|
|
26
|
-
// Only set up crash handler once to avoid memory leaks
|
|
27
28
|
if (this.crashHandlerSetup) {
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
|
-
this.consumer.on('consumer.crash', (event) => {
|
|
31
|
+
this.consumer.on('consumer.crash', (event) => __awaiter(this, void 0, void 0, function* () {
|
|
31
32
|
var _a;
|
|
32
33
|
const error = event.payload.error;
|
|
33
34
|
if ((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes('does not host this topic-partition')) {
|
|
34
35
|
console.warn(`[${this.topic}] Consumer partition error (non-fatal):`, error.message);
|
|
35
|
-
// Don't crash - this is often a transient error
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
this.crashRestartCount++;
|
|
39
|
+
const delay = Math.min(5000 * Math.pow(2, Math.min(this.crashRestartCount - 1, 5)), this.maxCrashRestartDelay);
|
|
40
|
+
console.error(`[${this.topic}] Consumer crashed (restart #${this.crashRestartCount}, ` +
|
|
41
|
+
`retrying in ${delay}ms):`, error);
|
|
42
|
+
this.isListening = false;
|
|
43
|
+
try {
|
|
44
|
+
yield this.consumer.disconnect();
|
|
45
|
+
}
|
|
46
|
+
catch (_) { /* best effort */ }
|
|
47
|
+
setTimeout(() => {
|
|
48
|
+
console.log(`[${this.topic}] Auto-restarting consumer after crash (attempt #${this.crashRestartCount})...`);
|
|
49
|
+
this.listen().catch((err) => {
|
|
50
|
+
console.error(`[${this.topic}] Auto-restart failed:`, err);
|
|
51
|
+
});
|
|
52
|
+
}, delay);
|
|
53
|
+
}));
|
|
40
54
|
this.crashHandlerSetup = true;
|
|
41
55
|
}
|
|
42
56
|
// Manual acknowledgment method
|