-
Notifications
You must be signed in to change notification settings - Fork 51
/
KafkaNode.ts
109 lines (95 loc) · 4.34 KB
/
KafkaNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { KafkaClient, Producer, Consumer } from 'kafka-node';
import IConfig from './IConfig';
import { Observable } from './common-types';
import { timeout } from 'promise-timeout';
import BasicParams from './BasicParams';
import { Observer } from 'rxjs';
import KafkaMessageStream, { IKafkaMessage } from './KafkaMessageStream';
import KafkaBase from './KafkaBase';
export default class Kafka extends KafkaBase {
// TODO: Close consumers + observers
private static _kafkaConnectionTimeoutInMs: number = 4500;
private static _kafkaRequestTimeoutInMs: number = 4500;
private static getKafkaClient(config: IConfig): KafkaClient {
// TODO: make sure what is the correct way to use kafka seed url, and check other constructor options here
const client = new KafkaClient({ kafkaHost: config.kafkaSeedUrls[0], connectTimeout: 6000, requestTimeout: 6000 });
return client;
}
private static getProducer(config: IConfig): Promise<Producer> {
const client = this.getKafkaClient(config);
const producer = new Producer(client);
const producerReadyPromise = new Promise<Producer>((resolve, reject) => {
producer.on('ready', () => resolve(producer));
producer.on('error', () => reject('Producer got error in connection'));
});
return timeout(producerReadyPromise, this._kafkaConnectionTimeoutInMs);
}
private static getConsumer(topicId: string, config: IConfig): Promise<Consumer> {
const client = this.getKafkaClient(config);
const consumer = new Consumer(
client,
[
{ topic: topicId },
],
{
groupId: topicId,
autoCommit: true,
},
);
const clientReadyPromise = new Promise<Consumer>((resolve, reject) => {
client.on('ready', () => resolve(consumer));
client.on('error', () => reject('client got error in connection'));
});
return timeout(clientReadyPromise, this._kafkaConnectionTimeoutInMs);
}
public static async createTopic(topicId: string, config: IConfig): Promise<void> {
const producer = await this.getProducer(config);
// CR_COMMENT: For me it sound like a name of a function, I think topicPromise is better
const createTopicPromise = new Promise<void>((resolve, reject) => {
producer.createTopics([topicId], true, (err: any, data: any) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return timeout(createTopicPromise, this._kafkaRequestTimeoutInMs);
}
public static async sendParams(topicId: string, basicParams: BasicParams, config: IConfig): Promise<void> {
const producer = await this.getProducer(config);
const payloads = [
{ topic: topicId, messages: basicParams.toJson()},
];
// CR_COMMENT: Same, but I couldn't think of a better name /:
const sendPromise = new Promise<void>((resolve, reject) => {
producer.send(payloads, (err: any, data: any) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return timeout(sendPromise, this._kafkaRequestTimeoutInMs);
}
public static async messages(topicId: string, config: IConfig): Promise<KafkaMessageStream> {
const consumer = await this.getConsumer(topicId, config);
const kafkaStream: Observable<IKafkaMessage> = Observable.create((observer: Observer<IKafkaMessage>) => {
consumer.on('message', (message) => {
try {
const messageString = message.value.toString();
const messageObject = JSON.parse(messageString);
const messageType = [messageObject.protocol, messageObject.type].join(':');
observer.next({
messageType,
contents: messageString,
});
} catch (error) {
observer.error(`error while trying to parse message. topic: ${topicId} error: ${error}, message: ${message}`);
}
});
});
return new KafkaMessageStream(kafkaStream);
}
}