mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Add connect methods to libraries.
This commit is contained in:
parent
186f484ff1
commit
e442617421
5 changed files with 59 additions and 28 deletions
14
lib/kafka.js
14
lib/kafka.js
|
|
@ -5,6 +5,10 @@ import { KAFKA, KAFKA_PRODUCER } from 'lib/db';
|
|||
|
||||
const log = debug('umami:kafka');
|
||||
|
||||
let kafka;
|
||||
let producer;
|
||||
const enabled = Boolean(process.env.KAFKA_URL && process.env.KAFKA_BROKER);
|
||||
|
||||
function getClient() {
|
||||
const { username, password } = new URL(process.env.KAFKA_URL);
|
||||
const brokers = process.env.KAFKA_BROKER.split(',');
|
||||
|
|
@ -61,7 +65,7 @@ function getDateFormat(date) {
|
|||
}
|
||||
|
||||
async function sendMessage(params, topic) {
|
||||
await getKafka();
|
||||
await connect();
|
||||
|
||||
await producer.send({
|
||||
topic,
|
||||
|
|
@ -74,7 +78,7 @@ async function sendMessage(params, topic) {
|
|||
});
|
||||
}
|
||||
|
||||
async function getKafka() {
|
||||
async function connect() {
|
||||
if (!kafka) {
|
||||
kafka = process.env.KAFKA_URL && process.env.KAFKA_BROKER && (global[KAFKA] || getClient());
|
||||
|
||||
|
|
@ -86,14 +90,12 @@ async function getKafka() {
|
|||
return kafka;
|
||||
}
|
||||
|
||||
// Initialization
|
||||
let kafka;
|
||||
let producer;
|
||||
|
||||
export default {
|
||||
enabled,
|
||||
client: kafka,
|
||||
producer,
|
||||
log,
|
||||
connect,
|
||||
getDateFormat,
|
||||
sendMessage,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue