@xrystal/core 3.22.0 → 3.22.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
|
@@ -38,6 +38,7 @@ export default class ConfigsService implements IService<any> {
|
|
|
38
38
|
systemService: SystemService;
|
|
39
39
|
});
|
|
40
40
|
onInit: ({}: {}) => Promise<void>;
|
|
41
|
+
private initializeKafkaInfrastructure;
|
|
41
42
|
setConfig(newConfigs: any): void;
|
|
42
43
|
_<K extends keyof IConfig>(key: K): IConfig[K];
|
|
43
44
|
get all(): IConfig;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { merge } from 'lodash';
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { Kafka, logLevel } from 'kafkajs';
|
|
4
5
|
import { Constants } from '../../utils';
|
|
5
6
|
export default class ConfigsService {
|
|
6
7
|
publicFolderName = Constants.publicFolderName;
|
|
@@ -62,6 +63,55 @@ export default class ConfigsService {
|
|
|
62
63
|
catch (e) {
|
|
63
64
|
// error
|
|
64
65
|
}
|
|
66
|
+
await this.initializeKafkaInfrastructure();
|
|
67
|
+
};
|
|
68
|
+
initializeKafkaInfrastructure = async () => {
|
|
69
|
+
const { kafkaBrokers, kafkaTopics, isKafkaPassive, serviceName } = {
|
|
70
|
+
isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
|
|
71
|
+
kafkaBrokers: process.env?.KAFKA_BROKERS,
|
|
72
|
+
kafkaTopics: [
|
|
73
|
+
...new Set([
|
|
74
|
+
Constants.kafkaLogsTopic,
|
|
75
|
+
...this.#config.kafkaTopics
|
|
76
|
+
])
|
|
77
|
+
],
|
|
78
|
+
serviceName: this.#systemService.tmp.configs.service
|
|
79
|
+
};
|
|
80
|
+
if (isKafkaPassive === true || !kafkaBrokers)
|
|
81
|
+
return;
|
|
82
|
+
const brokers = String(kafkaBrokers).split(",").map(b => b.trim());
|
|
83
|
+
const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
|
|
84
|
+
if (topicsToCreate.length === 0)
|
|
85
|
+
return;
|
|
86
|
+
const kafka = new Kafka({
|
|
87
|
+
clientId: `${serviceName}`,
|
|
88
|
+
brokers,
|
|
89
|
+
logLevel: logLevel.NOTHING
|
|
90
|
+
});
|
|
91
|
+
const admin = kafka.admin();
|
|
92
|
+
try {
|
|
93
|
+
await admin.connect();
|
|
94
|
+
const existingTopics = await admin.listTopics();
|
|
95
|
+
const newTopics = topicsToCreate
|
|
96
|
+
.filter(topic => !existingTopics.includes(topic))
|
|
97
|
+
.map(topic => ({
|
|
98
|
+
topic,
|
|
99
|
+
numPartitions: 1,
|
|
100
|
+
replicationFactor: 1
|
|
101
|
+
}));
|
|
102
|
+
if (newTopics.length > 0) {
|
|
103
|
+
await admin.createTopics({
|
|
104
|
+
waitForLeaders: true,
|
|
105
|
+
topics: newTopics
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
// => Error
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
await admin.disconnect();
|
|
114
|
+
}
|
|
65
115
|
};
|
|
66
116
|
setConfig(newConfigs) {
|
|
67
117
|
const mergedDObj = Object.assign(this.#config, newConfigs);
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Kafka, logLevel } from "kafkajs";
|
|
2
|
-
import { Constants } from "../../utils";
|
|
3
1
|
export default class SystemService {
|
|
4
2
|
_core = {};
|
|
5
3
|
_tmp = {};
|
|
@@ -8,55 +6,6 @@ export default class SystemService {
|
|
|
8
6
|
this._core = core;
|
|
9
7
|
this._tmp = this._core._;
|
|
10
8
|
await this._systemLoader({});
|
|
11
|
-
await this.initializeKafkaInfrastructure();
|
|
12
|
-
};
|
|
13
|
-
initializeKafkaInfrastructure = async () => {
|
|
14
|
-
const { kafkaBrokers, kafkaTopics, isKafkaPassive, serviceName } = {
|
|
15
|
-
isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
|
|
16
|
-
kafkaBrokers: process.env?.KAFKA_BROKERS,
|
|
17
|
-
kafkaTopics: [
|
|
18
|
-
...new Set([
|
|
19
|
-
Constants.kafkaLogsTopic,
|
|
20
|
-
...(process.env.KAFKA_TOPICS?.split(',').map(t => t.trim()).filter(Boolean) || [])
|
|
21
|
-
])
|
|
22
|
-
],
|
|
23
|
-
serviceName: this._tmp.configs.service
|
|
24
|
-
};
|
|
25
|
-
if (isKafkaPassive === true || !kafkaBrokers)
|
|
26
|
-
return;
|
|
27
|
-
const brokers = String(kafkaBrokers).split(",").map(b => b.trim());
|
|
28
|
-
const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
|
|
29
|
-
if (topicsToCreate.length === 0)
|
|
30
|
-
return;
|
|
31
|
-
const kafka = new Kafka({
|
|
32
|
-
clientId: `${serviceName}`,
|
|
33
|
-
brokers,
|
|
34
|
-
logLevel: logLevel.NOTHING
|
|
35
|
-
});
|
|
36
|
-
const admin = kafka.admin();
|
|
37
|
-
try {
|
|
38
|
-
await admin.connect();
|
|
39
|
-
const existingTopics = await admin.listTopics();
|
|
40
|
-
const newTopics = topicsToCreate
|
|
41
|
-
.filter(topic => !existingTopics.includes(topic))
|
|
42
|
-
.map(topic => ({
|
|
43
|
-
topic,
|
|
44
|
-
numPartitions: 1,
|
|
45
|
-
replicationFactor: 1
|
|
46
|
-
}));
|
|
47
|
-
if (newTopics.length > 0) {
|
|
48
|
-
await admin.createTopics({
|
|
49
|
-
waitForLeaders: true,
|
|
50
|
-
topics: newTopics
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
// => Error
|
|
56
|
-
}
|
|
57
|
-
finally {
|
|
58
|
-
await admin.disconnect();
|
|
59
|
-
}
|
|
60
9
|
};
|
|
61
10
|
_systemLoader = async ({}) => {
|
|
62
11
|
return;
|