@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.
@@ -4,7 +4,9 @@ export declare class KafkaClient {
4
4
  private consumer;
5
5
  private admin;
6
6
  private isConnected;
7
+ private isConsumerRunning;
7
8
  private config;
9
+ private subscriptions;
8
10
  constructor();
9
11
  private getKafkaConfig;
10
12
  connect(): Promise<void>;
@@ -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
- // // Notification events
134
- // {
135
- // topic: Subjects.NotificationEmail,
136
- // partitions: 4,
137
- // replicationFactor: 3,
138
- // },
139
- // { topic: Subjects.NotificationSMS, partitions: 4, replicationFactor: 3 },
140
- // { topic: Subjects.NotificationPush, partitions: 4, replicationFactor: 3 },
141
- // {
142
- // topic: Subjects.NotificationInApp,
143
- // partitions: 4,
144
- // replicationFactor: 3,
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
- yield this.consumer.subscribe({
214
- topic,
215
- fromBeginning: (options === null || options === void 0 ? void 0 : options.fromBeginning) || false,
216
- });
217
- console.log(`Subscribed to topic: ${topic}`);
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
- yield handler(event);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uber-clone/common",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -33,4 +33,4 @@
33
33
  "redlock": "^5.0.0-beta.2",
34
34
  "winston": "^3.18.3"
35
35
  }
36
- }
36
+ }