UNPKG

zamza

Version:

Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway

50 lines (49 loc) 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Debug = require("debug"); const debug = Debug("zamza:retryproducer"); const sinek_1 = require("sinek"); class RetryProducer { 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); } 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 = RetryProducer;