roach-storm
Version:
Apache Kafka to Google Pub/Sub Gateway, API controlled
65 lines (64 loc) • 2.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Debug = require("debug");
const debug = Debug("roach:producer");
const sinek_1 = require("sinek");
class Producer {
constructor(config, roachStorm) {
this.producedLately = 0;
this.config = config;
this.roachStorm = roachStorm;
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) {
throw new Error("Producer is not ready to produce yet.");
}
if (typeof value !== "string") {
value = JSON.stringify(value);
}
if (!Buffer.isBuffer(value)) {
value = Buffer.from(value);
}
this.roachStorm.metrics.inc(`kafka_msg_out`, 1, { topic });
this.producedLately++;
return this.producer.send(topic, value, partition, key);
}
produceTombstone(topic, key, partition) {
if (!this.producer) {
return Promise.resolve(null);
}
this.roachStorm.metrics.inc(`kafka_tomb_out, 1, { topic }}`);
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;