-
Notifications
You must be signed in to change notification settings - Fork 6
/
KafkaConsumer.js
197 lines (171 loc) · 5.24 KB
/
KafkaConsumer.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
195
196
197
/**
* The Kafka consumer service.
*/
const util = require('util');
const config = require('config');
const Kafka = require('no-kafka');
const healthcheck = require('topcoder-healthcheck-dropin');
const logger = require('./common/logger');
const busApi = require('topcoder-bus-api-wrapper');
const _ = require('lodash');
global.Promise = require('bluebird');
const busConfigObj = {
AUTH0_URL: config.AUTH0_URL,
AUTH0_AUDIENCE: config.AUTH0_AUDIENCE,
TOKEN_CACHE_TIME: config.TOKEN_CACHE_TIME,
AUTH0_CLIENT_ID: config.AUTH0_CLIENT_ID,
AUTH0_CLIENT_SECRET: config.AUTH0_CLIENT_SECRET,
BUSAPI_URL: config.BUSAPI_URL,
KAFKA_ERROR_TOPIC: config.KAFKA_ERROR_TOPIC,
AUTH0_PROXY_SERVER_URL: config.AUTH0_PROXY_SERVER_URL
};
const errorConfigObj = JSON.parse(JSON.stringify(busConfigObj));
errorConfigObj.LOG_LEVEL = config.LOG_LEVEL;
errorConfigObj.KAFKA_MESSAGE_ORIGINATOR = config.KAFKA_MESSAGE_ORIGINATOR;
errorConfigObj.POST_KAFKA_ERROR_ENABLED = true;
const busApiClient = busApi(busConfigObj);
/**
* Get kafka options.
* @returns {Object} kafka options
*/
function getKafkaOptions() {
const options = {
handlerConcurrency: config.KAFKA_CONCURRENCY,
connectionString: config.KAFKA_URL,
groupId: config.KAFKA_GROUP_ID
};
logger.info(`KAFKA Options - ${JSON.stringify(options)}`);
if (config.KAFKA_CLIENT_CERT && config.KAFKA_CLIENT_CERT_KEY) {
options.ssl = {
cert: config.KAFKA_CLIENT_CERT,
key: config.KAFKA_CLIENT_CERT_KEY
};
}
return options;
}
const consumer = new Kafka.GroupConsumer(getKafkaOptions());
/**
* Handle the messages from Kafka.
* @param {Object} submissionService the submission service
* @param {Array<Object>} messages the messages
* @param {String} topic the topic
* @param {Number} partition the partition
* @returns {Promise} the promise
* @private
*/
const handleMessages = (messageSet, topic, partition, submissionService) =>
Promise.each(messageSet, m => {
const message = m.message.value ? m.message.value.toString('utf8') : null;
const messageInfo = `Topic: ${topic}; Partition: ${partition}; Offset: ${m.offset}; Message: ${message}.`;
logger.info(`Handle Kafka event message; ${messageInfo}`);
if (!message) {
logger.error('Skipped null or empty event');
return;
}
let messageJSON;
try {
messageJSON = JSON.parse(message);
} catch (e) {
logger.error('Skipped Invalid message JSON');
logger.error(e);
// ignore the message
return;
}
if (!messageJSON) {
logger.error('Skipped null or empty event');
return;
}
if (messageJSON.topic !== topic) {
logger.error(
`Skipped the message topic "${messageJSON.topic}" doesn't match the Kafka topic ${topic}.`
);
// ignore the message
return;
}
return submissionService
.handle(messageJSON)
.then(() => {
logger.debug(`committing offset for ${JSON.stringify(messageJSON)}`);
consumer.commitOffset({
topic,
partition,
offset: m.offset
});
})
.catch(err => {
logger.error(`Failed to handle ${messageInfo}: ${err.message}`);
logger.debug(err);
logger.debug(
`Handling failed message; max retry count`
);
if (
_.get(messageJSON, 'payload.retryCount', 0) >
config.MESSAGE_RETRY_COUNT
) {
logger.error(err);
logger.debug(
`Error after processing the message ${config.MESSAGE_RETRY_COUNT} times, committing offset and sending message to error topic`
);
logger.debug(`sending error to error module`);
logger.error(err);
consumer.commitOffset({
topic,
partition,
offset: m.offset
});
} else {
logger.debug(`Reprocessing the message`);
let retryCount = messageJSON.payload.retryCount
? Number(messageJSON.payload.retryCount) + 1
: 1;
messageJSON.payload.retryCount = retryCount;
logger.debug(messageJSON);
busApiClient.postEvent(messageJSON);
}
});
});
/**
* Check if there is kafka connection alive
* @returns true if kafka connection alive, false otherwise
* @private
*/
function check() {
if (
!consumer.client.initialBrokers &&
!consumer.client.initialBrokers.length
) {
return false;
}
let connected = true;
consumer.client.initialBrokers.forEach(conn => {
logger.debug(`url ${conn.server()} - connected=${conn.connected}`);
connected = conn.connected & connected;
});
return connected;
}
/**
* Start kafka consumer.
* @param {Object} submissionService the submission service
* @param {Array<String>} topics the topics to subscribe
* @returns kafka consumer
*/
function startConsumer(submissionService, topics) {
consumer
.init([
{
subscriptions: topics,
handler: async (messageSet, topic, partition) =>
handleMessages(messageSet, topic, partition, submissionService)
}
])
.then(() => {
healthcheck.init([check]);
logger.debug('Consumer initialized successfully');
})
.catch(err => logger.error(err));
return consumer;
}
module.exports = {
getKafkaOptions,
startConsumer
};