@uber-clone/common 1.0.9 → 1.0.11
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.
|
@@ -15,6 +15,8 @@ const subjects_1 = require("./subjects");
|
|
|
15
15
|
class KafkaClient {
|
|
16
16
|
constructor() {
|
|
17
17
|
this.isConnected = false;
|
|
18
|
+
this.isConsumerRunning = false;
|
|
19
|
+
this.subscriptions = new Map();
|
|
18
20
|
this.config = this.getKafkaConfig();
|
|
19
21
|
this.kafka = new kafkajs_1.Kafka({
|
|
20
22
|
clientId: this.config.clientId,
|
|
@@ -130,19 +132,19 @@ class KafkaClient {
|
|
|
130
132
|
// { topic: Subjects.PaymentCompleted, partitions: 4, replicationFactor: 3 },
|
|
131
133
|
// { topic: Subjects.PaymentFailed, partitions: 4, replicationFactor: 3 },
|
|
132
134
|
// { topic: Subjects.PaymentRefunded, partitions: 4, replicationFactor: 3 },
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
135
|
+
// Notification events
|
|
136
|
+
{
|
|
137
|
+
topic: subjects_1.Subjects.NotificationEmail,
|
|
138
|
+
partitions: 4,
|
|
139
|
+
replicationFactor,
|
|
140
|
+
},
|
|
141
|
+
{ topic: subjects_1.Subjects.NotificationSMS, partitions: 4, replicationFactor },
|
|
142
|
+
{ topic: subjects_1.Subjects.NotificationPush, partitions: 4, replicationFactor },
|
|
143
|
+
{
|
|
144
|
+
topic: subjects_1.Subjects.NotificationInApp,
|
|
145
|
+
partitions: 4,
|
|
146
|
+
replicationFactor: 3,
|
|
147
|
+
},
|
|
146
148
|
];
|
|
147
149
|
try {
|
|
148
150
|
const existingTopics = yield this.admin.listTopics();
|
|
@@ -209,12 +211,23 @@ class KafkaClient {
|
|
|
209
211
|
subscribe(topic, handler, options) {
|
|
210
212
|
return __awaiter(this, void 0, void 0, function* () {
|
|
211
213
|
yield this.ensureConnection();
|
|
214
|
+
// Store the handler so we can restart the consumer with all topics if needed
|
|
215
|
+
this.subscriptions.set(topic, handler);
|
|
212
216
|
try {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
217
|
+
if (this.isConsumerRunning) {
|
|
218
|
+
// Consumer is already running — stop it, re-subscribe all topics, then restart
|
|
219
|
+
yield this.consumer.stop();
|
|
220
|
+
this.isConsumerRunning = false;
|
|
221
|
+
}
|
|
222
|
+
// Subscribe all known topics (KafkaJS requires this before consumer.run())
|
|
223
|
+
for (const [t,] of this.subscriptions) {
|
|
224
|
+
yield this.consumer.subscribe({
|
|
225
|
+
topic: t,
|
|
226
|
+
fromBeginning: (options === null || options === void 0 ? void 0 : options.fromBeginning) || false,
|
|
227
|
+
});
|
|
228
|
+
console.log(`Subscribed to topic: ${t}`);
|
|
229
|
+
}
|
|
230
|
+
// Start the consumer loop once with a dispatcher that routes to the correct handler
|
|
218
231
|
yield this.consumer.run({
|
|
219
232
|
eachMessage: (_a) => __awaiter(this, [_a], void 0, function* ({ topic, partition, message }) {
|
|
220
233
|
try {
|
|
@@ -227,14 +240,20 @@ class KafkaClient {
|
|
|
227
240
|
offset: message.offset,
|
|
228
241
|
eventType: event.type || "unknown",
|
|
229
242
|
});
|
|
230
|
-
|
|
243
|
+
const topicHandler = this.subscriptions.get(topic);
|
|
244
|
+
if (topicHandler) {
|
|
245
|
+
yield topicHandler(event);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
console.warn(`No handler registered for topic: ${topic}`);
|
|
249
|
+
}
|
|
231
250
|
}
|
|
232
251
|
catch (error) {
|
|
233
252
|
console.error(`Error processing message from ${topic}:${partition}:`, error);
|
|
234
|
-
// In production, you might want to implement dead letter queue or retry logic here
|
|
235
253
|
}
|
|
236
254
|
}),
|
|
237
255
|
});
|
|
256
|
+
this.isConsumerRunning = true;
|
|
238
257
|
}
|
|
239
258
|
catch (error) {
|
|
240
259
|
console.error(`Failed to subscribe to topic ${topic}:`, error);
|
package/package.json
CHANGED