UNPKG

@acadix/setup

Version:

Acadix Learning Management System backend application project setup

64 lines 2.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const logger_1 = require("../../logger"); const kafkajs_1 = require("kafkajs"); class KafkaManager { constructor(env) { this.env = env; const { host, port } = this.env.KAFKA; this.kafkaConfig = new kafkajs_1.Kafka({ clientId: this.env.SERVICE, brokers: [`${host}:${port}`], }); this.producer = this.kafkaConfig.producer(); this.consumer = this.kafkaConfig.consumer({ groupId: this.env.SERVICE }); this.logger = (0, logger_1.Logger)(this.env); } connectKafka() { return __awaiter(this, void 0, void 0, function* () { yield this.producer.connect(); yield this.consumer.connect(); this.logger.info("Kafka connected successfully"); }); } disconnectKafka() { return __awaiter(this, void 0, void 0, function* () { yield this.producer.disconnect(); yield this.consumer.disconnect(); this.logger.info("Kafka disconnected successfully"); }); } sendMessage(topic, message) { return __awaiter(this, void 0, void 0, function* () { return this.producer.send({ topic, messages: [{ value: message }] }); }); } connectMessage(topics) { return __awaiter(this, void 0, void 0, function* () { yield Promise.all(topics.map((topic) => __awaiter(this, void 0, void 0, function* () { yield this.consumer.subscribe({ topic, fromBeginning: true }); }))); yield this.consumer.run({ eachMessage: ({ topic, partition, message }) => __awaiter(this, void 0, void 0, function* () { if (!message || !message.value) { return; } const data = JSON.parse(message.value.toString()); this.logger.info(data, topic, partition); return data; }), }); }); } } exports.default = KafkaManager; //# sourceMappingURL=connectKafka.js.map