@xrystal/core 3.27.9 → 3.28.1
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.
package/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import Configs from "../configs";
|
|
2
2
|
import System from "../system";
|
|
3
|
-
import KafkaForCoreSeeder from "./seeder";
|
|
4
3
|
import { IProvide } from "../../utils";
|
|
5
4
|
type KafkaInstanceType = {
|
|
6
5
|
clientId: string;
|
|
@@ -16,13 +15,13 @@ export default class KafkaForCore implements IProvide<any> {
|
|
|
16
15
|
private brokers;
|
|
17
16
|
private username;
|
|
18
17
|
private password;
|
|
19
|
-
constructor({ system, configs,
|
|
18
|
+
constructor({ system, configs, }: {
|
|
20
19
|
system: System;
|
|
21
20
|
configs: Configs;
|
|
22
|
-
kafkaForCoreSeeder: KafkaForCoreSeeder;
|
|
23
21
|
});
|
|
24
22
|
onInit: ({}: {}) => Promise<void>;
|
|
25
23
|
kafkaLoader: ({ clientId, brokers, username, password }: KafkaInstanceType) => Promise<any>;
|
|
24
|
+
initializeKafkaInfrastructure: () => Promise<void>;
|
|
26
25
|
sendMessage: (topic: string, message: any) => Promise<void>;
|
|
27
26
|
get producer(): any;
|
|
28
27
|
get instance(): any;
|
|
@@ -7,12 +7,10 @@ export default class KafkaForCore {
|
|
|
7
7
|
username = null;
|
|
8
8
|
password = null;
|
|
9
9
|
#system;
|
|
10
|
-
#kafkaForCoreSeeder;
|
|
11
10
|
#configs;
|
|
12
|
-
constructor({ system, configs,
|
|
11
|
+
constructor({ system, configs, }) {
|
|
13
12
|
this.#system = system;
|
|
14
13
|
this.#configs = configs;
|
|
15
|
-
this.#kafkaForCoreSeeder = kafkaForCoreSeeder;
|
|
16
14
|
}
|
|
17
15
|
onInit = async ({}) => {
|
|
18
16
|
this.clientId = this.#configs.all?.kafkaClientId;
|
|
@@ -34,7 +32,7 @@ export default class KafkaForCore {
|
|
|
34
32
|
username: this.username,
|
|
35
33
|
password: this.password
|
|
36
34
|
});
|
|
37
|
-
await this
|
|
35
|
+
await this.initializeKafkaInfrastructure();
|
|
38
36
|
};
|
|
39
37
|
kafkaLoader = async ({ clientId, brokers, username, password }) => {
|
|
40
38
|
const kafkaConfig = {
|
|
@@ -64,6 +62,46 @@ export default class KafkaForCore {
|
|
|
64
62
|
console.error(`Kafka ${error}`);
|
|
65
63
|
}
|
|
66
64
|
};
|
|
65
|
+
initializeKafkaInfrastructure = async () => {
|
|
66
|
+
const { kafkaBrokers, kafkaTopics, isKafkaPassive, } = {
|
|
67
|
+
isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
|
|
68
|
+
kafkaBrokers: process.env?.KAFKA_BROKERS,
|
|
69
|
+
kafkaTopics: [
|
|
70
|
+
...new Set([
|
|
71
|
+
...this.#configs.all.kafkaTopics
|
|
72
|
+
])
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
if (isKafkaPassive === true || !kafkaBrokers)
|
|
76
|
+
return;
|
|
77
|
+
const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
|
|
78
|
+
if (topicsToCreate.length === 0)
|
|
79
|
+
return;
|
|
80
|
+
const admin = this._instance.admin();
|
|
81
|
+
try {
|
|
82
|
+
await admin.connect();
|
|
83
|
+
const existingTopics = await admin.listTopics();
|
|
84
|
+
const newTopics = topicsToCreate
|
|
85
|
+
.filter(topic => !existingTopics.includes(topic))
|
|
86
|
+
.map(topic => ({
|
|
87
|
+
topic,
|
|
88
|
+
numPartitions: 1,
|
|
89
|
+
replicationFactor: 1
|
|
90
|
+
}));
|
|
91
|
+
if (newTopics.length > 0) {
|
|
92
|
+
await admin.createTopics({
|
|
93
|
+
waitForLeaders: true,
|
|
94
|
+
topics: newTopics
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
console.log('comming', error);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
await admin.disconnect();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
67
105
|
// => Helpers
|
|
68
106
|
sendMessage = async (topic, message) => {
|
|
69
107
|
if (!this.producer)
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Kafka } from "kafkajs";
|
|
2
|
-
import Configs from "source/loader/configs";
|
|
3
|
-
import { Logger } from "../../index";
|
|
4
|
-
export default class KafkaForCoreSeeder {
|
|
5
|
-
#private;
|
|
6
|
-
constructor({ logger, kafka, cache, configs, }: {
|
|
7
|
-
logger: Logger;
|
|
8
|
-
kafka: Kafka;
|
|
9
|
-
cache: Cache;
|
|
10
|
-
configs: Configs;
|
|
11
|
-
});
|
|
12
|
-
initializeKafkaInfrastructure: () => Promise<void>;
|
|
13
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export default class KafkaForCoreSeeder {
|
|
2
|
-
#configs;
|
|
3
|
-
#kafka;
|
|
4
|
-
#cache;
|
|
5
|
-
#logger;
|
|
6
|
-
constructor({ logger, kafka, cache, configs, }) {
|
|
7
|
-
this.#logger = logger;
|
|
8
|
-
this.#kafka = kafka;
|
|
9
|
-
this.#cache = cache;
|
|
10
|
-
this.#configs = configs;
|
|
11
|
-
}
|
|
12
|
-
initializeKafkaInfrastructure = async () => {
|
|
13
|
-
const { kafkaBrokers, kafkaTopics, isKafkaPassive, } = {
|
|
14
|
-
isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
|
|
15
|
-
kafkaBrokers: process.env?.KAFKA_BROKERS,
|
|
16
|
-
kafkaTopics: [
|
|
17
|
-
...new Set([
|
|
18
|
-
...this.#configs.all.kafkaTopics
|
|
19
|
-
])
|
|
20
|
-
],
|
|
21
|
-
};
|
|
22
|
-
if (isKafkaPassive === true || !kafkaBrokers)
|
|
23
|
-
return;
|
|
24
|
-
const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
|
|
25
|
-
if (topicsToCreate.length === 0)
|
|
26
|
-
return;
|
|
27
|
-
const admin = this.#kafka.admin();
|
|
28
|
-
try {
|
|
29
|
-
await admin.connect();
|
|
30
|
-
const existingTopics = await admin.listTopics();
|
|
31
|
-
const newTopics = topicsToCreate
|
|
32
|
-
.filter(topic => !existingTopics.includes(topic))
|
|
33
|
-
.map(topic => ({
|
|
34
|
-
topic,
|
|
35
|
-
numPartitions: 1,
|
|
36
|
-
replicationFactor: 1
|
|
37
|
-
}));
|
|
38
|
-
if (newTopics.length > 0) {
|
|
39
|
-
await admin.createTopics({
|
|
40
|
-
waitForLeaders: true,
|
|
41
|
-
topics: newTopics
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
// => Error
|
|
47
|
-
}
|
|
48
|
-
finally {
|
|
49
|
-
await admin.disconnect();
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|