zamza
Version:
Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway
66 lines (65 loc) • 2.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const EventEmitter = require("events");
const Debug = require("debug");
const debug = Debug("zamza:mongopoller");
const Discovery_1 = require("../kafka/Discovery");
class MongoPoller extends EventEmitter {
constructor(mongoWrapper, metrics) {
super();
this.topicConfigModel = mongoWrapper.getTopicConfig();
this.hookModel = mongoWrapper.getHook();
this.metrics = metrics;
this.intv = null;
this.topicConfigHash = 0;
this.collected = {
topicConfigs: [],
hooks: [],
};
}
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.topic);
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);
}
const hooks = await this.hookModel.list();
this.metrics.set("configured_hooks", hooks.length);
this.emit("hooks-changed", hooks); // currently we just always emit
this.collected = Object.assign(this.collected, {
topicConfigs,
hooks,
});
this.metrics.inc("job_poll_ran_success");
}
}
exports.default = MongoPoller;