forked from fwang7/node-red-contrib-kafka-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka.js
194 lines (149 loc) · 5.43 KB
/
kafka.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Created by fwang1 on 3/25/15.
*/
module.exports = function(RED) {
/*
* Kafka Producer
* Parameters:
- topics
- zkquorum(example: zkquorum = “[host]:2181")
*/
function kafkaNode(config) {
RED.nodes.createNode(this,config);
var topic = config.topic;
var clusterZookeeper = config.zkquorum;
var node = this;
var kafka = require('kafka-node');
var HighLevelProducer = kafka.HighLevelProducer;
var Client = kafka.Client;
var topics = config.topics;
var client = new Client(clusterZookeeper);
try {
this.on("input", function(msg) {
var payloads = [];
// check if multiple topics
if (topics.indexOf(",") > -1){
var topicArry = topics.split(',');
for (var i = 0; i < topicArry.length; i++) {
payloads.push({topic: topicArry[i], messages: msg.payload});
}
}
else {
payloads = [{topic: topics, messages: msg.payload}];
}
producer.send(payloads, function(err, data){
if (err){
node.error(err);
}
node.log("Message Sent: " + data);
});
});
}
catch(e) {
node.error(e);
}
var producer = new HighLevelProducer(client);
}
RED.nodes.registerType("kafka",kafkaNode);
/*
* Kafka Consumer
* Parameters:
- topics
- groupId
- zkquorum(example: zkquorum = “[host]:2181")
*/
function kafkaInNode(config) {
RED.nodes.createNode(this,config);
var node = this;
var kafka = require('kafka-node');
var topics = String(config.topics);
var clusterZookeeper = config.zkquorum;
var groupId = config.groupId;
var topicJSONArry = [];
// check if multiple topics
if (topics.indexOf(",") > -1){
var topicArry = topics.split(',');
console.log(topicArry);
console.log(topicArry.length);
for (var i = 0; i < topicArry.length; i++) {
console.log(topicArry[i]);
topicJSONArry.push({topic: topicArry[i]});
}
topics = topicJSONArry;
}
else {
topics = [{topic:topics}];
}
var options = {
groupId: groupId,
autoCommit: config.autoCommit,
autoCommitMsgCount: 10
};
var currentConsumer = new retryConsumer(node, kafka, topics, clusterZookeeper, options);
currentConsumer.run();
}
retryConsumer.prototype.kickstart= function(){
var self= this;
self.dead = true;//make sure
setTimeout(function(){
self.node.currentConsumer = new retryConsumer(self.node, self.kafka, self.topics, self.clusterZookeeper, self.options);
self.node.currentConsumer.run();
},self.restartWaitTime);
try {
if (self.consumer) {
var tc = self.consumer; //switchroo make sure we set it to null before doing close
self.consumer = null;
tc.close();
}
} catch (e) {
console.log('exception while closing:' +e);
}
};
function retryConsumer(node, kafka, topics, clusterZookeeper, options){
var self = this;
self.node = node;
self.clusterZookeeper = clusterZookeeper;
self.kafka = kafka;
self.client = new kafka.Client(clusterZookeeper);
self.dead = false; //a safety check to make sure that once we think this instance is dead we don't use it anymore.
self.topics = topics;
self.options = options;
self.HighLevelConsumer = kafka.HighLevelConsumer;
self.restartWaitTime = 5000;
}
retryConsumer.prototype.run= function run(){
var self = this;
try {
var consumer = new self.HighLevelConsumer(self.client, self.topics, self.options);
self.consumer = consumer;
console.log("Consumer created...");
//helps to uncomment these for issues.
//consumer.on('done', function(message){console.log('done:'+ message);})
//consumer.on('ready', function(message){console.log('ready:'+ message);})
//consumer.on('connect', function(message){console.log('connect:'+ message);})
//consumer.on('close', function(message){console.log('close:'+ message);})
consumer.on('message', function (message) {
if(self.dead) return;
console.log(message);
self.node.log(message);
var msg = {payload: message};
self.node.send(msg);
});
consumer.on('error', function (err) {
if(self.dead)return;
self.dead = true;
console.log("CONSOLE ERROR" +err);
self.node.error("CONSOLE ERROR" +err);
self.kickstart();
});
}
catch(e){
if(self.dead)return;
self.dead = true;
console.log("catch" +e);
self.node.error("Catch error" +e);
self.kickstart();
}
};
RED.nodes.registerType("kafka in", kafkaInNode);
};