UNPKG

roach-storm

Version:

Apache Kafka to Google Pub/Sub Gateway, API controlled

60 lines (59 loc) 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const EventEmitter = require("events"); const Debug = require("debug"); const debug = Debug("roach:mongopoller"); const Discovery_1 = require("../kafka/Discovery"); class MongoPoller extends EventEmitter { constructor(mongoWrapper, metrics) { super(); this.topicConfigModel = mongoWrapper.getTopicConfig(); this.metrics = metrics; this.intv = null; this.topicConfigHash = 0; this.collected = { topicConfigs: [], }; } async start(intervalMs = 15000) { this.close(); this.intv = setInterval(() => { this.onInterval() .then(() => { this.emit("updated", this.collected); }) .catch((error) => { this.emit("error", error); }); }, intervalMs); // poll once initially await this.onInterval(); this.emit("updated", this.collected); debug("Initial poll done."); } close() { if (this.intv) { clearInterval(this.intv); } } getCollected() { return this.collected; } async onInterval() { this.metrics.inc("job_poll_ran"); const topicConfigs = await this.topicConfigModel.list(); this.metrics.set("configured_topics", topicConfigs.length); const topics = topicConfigs.map((topicConfig) => topicConfig.sourceTopic); const newTopicConfigHash = Discovery_1.default.arrayToFixedHash(topics); if (this.topicConfigHash !== newTopicConfigHash) { this.topicConfigHash = newTopicConfigHash; this.metrics.inc("configured_topics_changed"); this.emit("topic-config-changed", topics); } this.collected = Object.assign(this.collected, { topicConfigs, }); this.metrics.inc("job_poll_ran_success"); } } exports.default = MongoPoller;