zamza
Version:
Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway
57 lines (56 loc) • 1.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Debug = require("debug");
const debug = Debug("zamza:producer");
const sinek_1 = require("sinek");
class Producer {
constructor(config, zamza) {
this.producedLately = 0;
this.config = config;
this.zamza = zamza;
this.producer = null;
this.intv = setInterval(() => {
if (this.producedLately > 0) {
debug("Produced", this.producedLately, "messages lately");
this.producedLately = 0;
}
}, 45000);
}
async start() {
debug("Connecting..");
this.producer = new sinek_1.NProducer(this.config.producer, null, this.config.defaultPartitions);
await this.producer.connect();
debug("Connected.");
}
produceMessage(topic, partition = null, key = null, value = null) {
if (!this.producer) {
return Promise.resolve(null);
}
this.producedLately++;
return this.producer.send(topic, value, partition, key);
}
produceTombstone(topic, key, partition) {
if (!this.producer) {
return Promise.resolve(null);
}
this.producedLately++;
return this.producer.tombstone(topic, key, partition);
}
getKafkaStats() {
return this.producer ? this.producer.getStats() : {};
}
getTopicMetadata() {
return this.producer ? this.producer.getMetadata(2500) : Promise.resolve({});
}
async close() {
debug("Closing..");
if (this.intv) {
clearInterval(this.intv);
}
if (this.producer) {
this.producer.close();
this.producer = null;
}
}
}
exports.default = Producer;