@aichatwar/shared 1.0.93 → 1.0.94
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.
|
@@ -6,6 +6,8 @@ export declare abstract class Listener<T extends BaseEvent> {
|
|
|
6
6
|
abstract onMessage(data: T['data'], payload: EachMessagePayload): Promise<void>;
|
|
7
7
|
protected consumer: Consumer;
|
|
8
8
|
protected ackDeadline: number;
|
|
9
|
+
private currentPayload?;
|
|
9
10
|
constructor(consumer: Consumer);
|
|
11
|
+
protected ack(): Promise<void>;
|
|
10
12
|
listen(): Promise<void>;
|
|
11
13
|
}
|
|
@@ -15,6 +15,20 @@ class Listener {
|
|
|
15
15
|
this.ackDeadline = 5 * 1000; // 5 seconds
|
|
16
16
|
this.consumer = consumer;
|
|
17
17
|
}
|
|
18
|
+
// Manual acknowledgment method
|
|
19
|
+
ack() {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
if (!this.currentPayload) {
|
|
22
|
+
throw new Error('No current payload to acknowledge');
|
|
23
|
+
}
|
|
24
|
+
yield this.consumer.commitOffsets([{
|
|
25
|
+
topic: this.currentPayload.topic,
|
|
26
|
+
partition: this.currentPayload.partition,
|
|
27
|
+
offset: (BigInt(this.currentPayload.message.offset) + BigInt(1)).toString()
|
|
28
|
+
}]);
|
|
29
|
+
console.log(`Message manually acknowledged for topic: ${this.topic}`);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
18
32
|
listen() {
|
|
19
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
34
|
yield this.consumer.connect();
|
|
@@ -28,22 +42,23 @@ class Listener {
|
|
|
28
42
|
if (!payload.message.value)
|
|
29
43
|
return;
|
|
30
44
|
console.log(`Message received -> topic: ${this.topic}, groupId: ${this.groupId}`);
|
|
45
|
+
// Store current payload for manual ack
|
|
46
|
+
this.currentPayload = payload;
|
|
31
47
|
try {
|
|
32
48
|
const data = JSON.parse(payload.message.value.toString());
|
|
33
49
|
yield this.onMessage(data, payload);
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
topic: payload.topic,
|
|
37
|
-
partition: payload.partition,
|
|
38
|
-
offset: (BigInt(payload.message.offset) + BigInt(1)).toString()
|
|
39
|
-
}]);
|
|
40
|
-
console.log(`Message processed and acknowledged for topic: ${this.topic}`);
|
|
50
|
+
// Note: Child listeners must call this.ack() manually after successful processing
|
|
51
|
+
// If they don't call ack(), the message will be redelivered
|
|
41
52
|
}
|
|
42
53
|
catch (error) {
|
|
43
54
|
console.error(`Error processing message for topic: ${this.topic}`, error);
|
|
44
55
|
// In case of error, we don't commit the offset, so the message will be redelivered
|
|
45
56
|
throw error;
|
|
46
57
|
}
|
|
58
|
+
finally {
|
|
59
|
+
// Clear the current payload
|
|
60
|
+
this.currentPayload = undefined;
|
|
61
|
+
}
|
|
47
62
|
})
|
|
48
63
|
});
|
|
49
64
|
});
|