Spaces:
Sleeping

File size: 2,630 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03d6d9d
4e83772
 
 
 
03d6d9d
 
 
4e83772
 
 
 
03d6d9d
 
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { IKafkaEventConsumer } from "@/interfaces/consumers/IKafkaEventConsumer.js";
import { Consumer, Kafka } from "kafkajs";
import universalKafkaHandler, { getAllTopics, EVENT_REGISTRY, getDeploymentLogsTopic } from "@/events/regitry.js";

class KafkaEventConsumer implements IKafkaEventConsumer {
	private kafka: Kafka;
	private logsConsumer: Consumer;
	private analyticsConsumer: Consumer;
	private isRunning = false;

	constructor(kafka: Kafka) {
		this.kafka = kafka;
		this.logsConsumer = this.kafka.consumer({
			groupId: "lynfera-logs-group",
			sessionTimeout: 45000,
			heartbeatInterval: 3000,
		});
		this.analyticsConsumer = this.kafka.consumer({
			groupId: "lynfera-analytics-group",
			sessionTimeout: 45000,
			heartbeatInterval: 3000,
		});
	}
	async start() {
		if (this.isRunning) {
			console.log("Kafka already running");
			return;
		}
		try {
			await Promise.all([this.logsConsumer.connect(), this.analyticsConsumer.connect()]);
			console.log(" Kafka consumers connected");

			const [logsTopics, analyticsTopics] = getAllTopics();
			await this.logsConsumer.subscribe({ topics: logsTopics, });
			await this.analyticsConsumer.subscribe({ topics: analyticsTopics });
			console.log(` Subscribed to topics:`, [...logsTopics, ...analyticsTopics]);

			await this.logsConsumer.run({
				autoCommit: true,
				autoCommitInterval: 10000,
				eachBatchAutoResolve: true,
				eachBatch: universalKafkaHandler,
			});
			await this.analyticsConsumer.run({
				autoCommit: true,
				autoCommitInterval: 10000,
				eachBatchAutoResolve: true,
				eachBatch: universalKafkaHandler,
			});

			this.isRunning = true;
			console.log("-------------------------------Kafka consumers started successfully-------------------------------");
		} catch (error) {
			console.error("Failed to start Kafka consumers:", error);
			throw error;
		}
	}
	getDeploymentLogsTopic() {
		return getDeploymentLogsTopic();
	}
	pauseLogs() {
		console.log(this.getDeploymentLogsTopic(), "Pasuing logs topics....");
		this.logsConsumer.pause([{ topic: this.getDeploymentLogsTopic() }]);
	}
	resumeLogs() {
		console.log(this.getDeploymentLogsTopic(), "Resume logs topics....");
		this.logsConsumer.resume([{ topic: this.getDeploymentLogsTopic() }]);
	}

	async stop(): Promise<void> {
		if (!this.isRunning) {
			return;
		}

		try {
			await Promise.all([this.logsConsumer.disconnect(), this.analyticsConsumer.disconnect()]);
			this.isRunning = false;
			console.log("Kafka consumer stopped");
		} catch (error) {
			console.error("Error stopping Kafka consumer:", error);
			throw error;
		}
	}
}

export default KafkaEventConsumer;