@aichatwar/shared 1.0.85 → 1.0.86

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.
@@ -1,9 +1,15 @@
1
- import { Kafka } from "kafkajs";
1
+ import { Kafka, Producer } from "kafkajs";
2
2
  import { BaseEvent } from '../baseEvent';
3
3
  export declare abstract class Publisher<T extends BaseEvent> {
4
- abstract topic: T["subject"];
5
- private producer;
6
- constructor(client: Kafka);
4
+ abstract topic: T['subject'];
5
+ protected producer: Producer;
6
+ constructor(kafka: Kafka);
7
+ /**
8
+ * Connect this producer once, typically during service startup
9
+ */
7
10
  connect(): Promise<void>;
8
- publish(data: T["data"]): Promise<void>;
11
+ /**
12
+ * Publish an event to Kafka with retry-on-disconnect logic
13
+ */
14
+ publish(data: T['data']): Promise<void>;
9
15
  }
@@ -10,23 +10,49 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Publisher = void 0;
13
- // Updated event to base event
14
13
  class Publisher {
15
- constructor(client) {
16
- this.producer = client.producer();
14
+ constructor(kafka) {
15
+ // Each Publisher gets its own producer from injected Kafka instance
16
+ this.producer = kafka.producer();
17
17
  }
18
+ /**
19
+ * Connect this producer once, typically during service startup
20
+ */
18
21
  connect() {
19
22
  return __awaiter(this, void 0, void 0, function* () {
23
+ if (!this.producer)
24
+ throw new Error('Producer not initialized');
20
25
  yield this.producer.connect();
26
+ console.log(`✅ Kafka Producer connected for topic "${this.topic}"`);
21
27
  });
22
28
  }
29
+ /**
30
+ * Publish an event to Kafka with retry-on-disconnect logic
31
+ */
23
32
  publish(data) {
24
33
  return __awaiter(this, void 0, void 0, function* () {
25
- yield this.producer.send({
26
- topic: this.topic,
27
- messages: [{ value: JSON.stringify(data) }],
28
- });
29
- console.log(`Published to topic ${this.topic}`);
34
+ try {
35
+ yield this.producer.send({
36
+ topic: this.topic,
37
+ messages: [{ value: JSON.stringify(data) }],
38
+ });
39
+ console.log(`📤 Event published to "${this.topic}"`, data);
40
+ }
41
+ catch (err) {
42
+ console.error(`❌ Failed to publish to ${this.topic}`, err);
43
+ if (err.message.includes('not connected') || err.message.includes('disconnected')) {
44
+ console.warn('⚠ Producer disconnected — retrying connection...');
45
+ yield this.producer.connect();
46
+ yield this.producer.send({
47
+ topic: this.topic,
48
+ messages: [{ value: JSON.stringify(data) }],
49
+ });
50
+ console.log(`📤 Event republished to "${this.topic}" after reconnect`);
51
+ }
52
+ else {
53
+ throw err;
54
+ }
55
+ }
30
56
  });
31
57
  }
32
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aichatwar/shared",
3
- "version": "1.0.85",
3
+ "version": "1.0.86",
4
4
  "main": "./build/index.js",
5
5
  "typs": "./build/index.d.ts",
6
6
  "files": [