-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hovoodd
committed
Oct 14, 2020
1 parent
c860b40
commit 331932a
Showing
14 changed files
with
124 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,41 @@ | ||
const { Kafka } = require('kafkajs'); | ||
const KafkaJS = require('kafkajs'); | ||
|
||
const { KafkaProcessor } = require('./kafka-processor'); | ||
|
||
const kafka = new Kafka({ | ||
clientId: 'ship', | ||
brokers: ['kafka:9092'], | ||
}); | ||
class Kafka { | ||
constructor(config) { | ||
this.kafka = new KafkaJS.Kafka(config); | ||
|
||
const processors = { | ||
user: new KafkaProcessor(kafka, { groupId: 'ship' }, { topic: 'user' }), | ||
}; | ||
this.producer = this.kafka.producer(); | ||
this.processors = {}; | ||
} | ||
|
||
async function run() { | ||
await Promise.all(Object.values(processors).map((p) => p.run())); | ||
} | ||
addProcessor(topic, consumerConfig, subscribeConfig = { topic }) { | ||
if (this.processors[topic]) { | ||
throw new Error(`Processor for "${topic}" topic already exists`); | ||
} | ||
|
||
this.processors[topic] = new KafkaProcessor(this.kafka, consumerConfig, subscribeConfig); | ||
return this; | ||
} | ||
|
||
async run() { | ||
await this.producer.connect(); | ||
await Promise.all(Object.values(this.processors).map((p) => p.run())); | ||
} | ||
|
||
async function send({ event, data, ...record }) { | ||
const producer = kafka.producer(); | ||
await producer.connect(); | ||
await producer.send({ | ||
...record, | ||
messages: [ | ||
{ value: JSON.stringify({ event, data }) }, | ||
], | ||
}); | ||
await producer.disconnect(); | ||
async send({ event, data, ...record }) { | ||
await this.producer.send({ | ||
...record, | ||
messages: [ | ||
{ value: JSON.stringify({ event, data }) }, | ||
], | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
kafka, | ||
processors, | ||
run, | ||
send, | ||
}; | ||
const kafka = new Kafka({ clientId: 'ship', brokers: ['kafka:9092'] }); | ||
|
||
kafka.addProcessor('user', { groupId: 'ship' }); | ||
|
||
module.exports = kafka; |
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
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,42 @@ | ||
class NodeMongoKafkaEmitter { | ||
constructor(_topic, _kafka, _logger = console) { | ||
this.topic = _topic; | ||
this.kafka = _kafka; | ||
this.logger = _logger; | ||
|
||
this.processor = this.kafka.processors[this.topic]; | ||
if (!this.processor) { | ||
throw new Error(`Processor for "${this.topic}" topic doesn't exist`); | ||
} | ||
} | ||
|
||
castEvent(event) { | ||
return `${this.topic}:${event}`; | ||
} | ||
|
||
async emit(event, data) { | ||
try { | ||
await this.kafka.send({ | ||
topic: this.topic, | ||
event: this.castEvent(event), | ||
data, | ||
}); | ||
} catch (error) { | ||
this.logger.error(error); | ||
} | ||
} | ||
|
||
on(event, listener) { | ||
this.processor.on(this.castEvent(event), async (message) => { | ||
await listener(message.data); | ||
}); | ||
} | ||
|
||
once(event, listener) { | ||
this.processor.once(this.castEvent(event), async (message) => { | ||
await listener(message.data); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = NodeMongoKafkaEmitter; |
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
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
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