-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1188 from jembi/PLAT-707-kafka-integration
PLAT-707 Add kafka integration
- Loading branch information
Showing
18 changed files
with
861 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import logger from 'winston' | ||
import {Kafka, logLevel} from 'kafkajs' | ||
import {config} from './config' | ||
|
||
config.router = config.get('router') | ||
|
||
// Customize Kafka logs | ||
function kafkaLogger() { | ||
const toWinstonLogLevel = level => { | ||
switch (level) { | ||
case logLevel.ERROR: | ||
case logLevel.NOTHING: | ||
return 'error' | ||
case logLevel.WARN: | ||
return 'warn' | ||
case logLevel.INFO: | ||
return 'info' | ||
case logLevel.DEBUG: | ||
return 'debug' | ||
} | ||
} | ||
return ({level, log}) => { | ||
const {message, ...extra} = log | ||
logger[toWinstonLogLevel(level)]({ | ||
message, | ||
extra | ||
}) | ||
} | ||
} | ||
|
||
export class KafkaProducer { | ||
_producer = null | ||
_isConnected = false | ||
|
||
constructor(clientId, timeout) { | ||
if (clientId) { | ||
let brokers = config.router.kafkaBrokers | ||
brokers = brokers.replace(/"/g, '').split(',') | ||
|
||
const kafka = new Kafka({ | ||
brokers: brokers, | ||
clientId: clientId, | ||
requestTimeout: timeout, | ||
connectionTimeout: timeout, | ||
logLevel: logLevel.DEBUG, | ||
logCreator: kafkaLogger | ||
}) | ||
|
||
this._producer = kafka.producer() | ||
|
||
this._producer.on(this._producer.events.DISCONNECT, () => { | ||
this._isConnected = false | ||
}) | ||
} | ||
} | ||
|
||
get isConnected() { | ||
return this._isConnected | ||
} | ||
|
||
get producer() { | ||
return this._producer | ||
} | ||
|
||
async connect() { | ||
// Not catching the error to throw the original error message | ||
await this._producer.connect() | ||
this._isConnected = true | ||
} | ||
|
||
async disconnect() { | ||
try { | ||
await this._producer.disconnect() | ||
this._isConnected = false | ||
} catch (err) { | ||
logger.error(err.message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {KafkaProducer} from './kafkaProducer.js' | ||
|
||
export class KafkaProducerManager { | ||
static kafkaSet = {} | ||
|
||
static async getProducer(channelName, clientId, timeout) { | ||
const kafkaInstance = this.findOrAddConnection( | ||
channelName, | ||
clientId, | ||
timeout | ||
) | ||
if (!kafkaInstance.isConnected) await kafkaInstance.connect() | ||
|
||
return kafkaInstance.producer | ||
} | ||
|
||
static findOrAddConnection(channelName, clientId, timeout) { | ||
let kafkaInstance = this.getKafkaInstance(channelName, clientId, timeout) | ||
if (!kafkaInstance) { | ||
kafkaInstance = new KafkaProducer(clientId, timeout) | ||
this.kafkaSet[`urn:${channelName}:${clientId}:${timeout}`] = kafkaInstance | ||
} | ||
|
||
return kafkaInstance | ||
} | ||
|
||
static async removeConnection(channelName, clientId, timeout) { | ||
const kafkaInstance = this.getKafkaInstance(channelName, clientId, timeout) | ||
|
||
if (kafkaInstance) { | ||
if (kafkaInstance.isConnected) await kafkaInstance.disconnect() | ||
delete this.kafkaSet[`urn:${channelName}:${clientId}:${timeout}`] | ||
} | ||
} | ||
|
||
static getKafkaInstance(channelName, clientId, timeout) { | ||
return this.kafkaSet[`urn:${channelName}:${clientId}:${timeout}`] | ||
} | ||
} |
Oops, something went wrong.