redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
76 lines • 3.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigSync = void 0;
const redis_smq_common_1 = require("redis-smq-common");
const configuration_js_1 = require("./configuration.js");
const event_multiplexer_js_1 = require("../event-bus/event-multiplexer.js");
const internal_event_bus_js_1 = require("../event-bus/internal-event-bus.js");
class ConfigSync extends redis_smq_common_1.Runnable {
constructor() {
super();
this.onLocalConfigUpdated = (cfg, version) => {
this.logger.debug('Publishing local config update...', cfg);
event_multiplexer_js_1.EventMultiplexer.getInstance().publish('configuration.updated', cfg, version);
};
this.onEventBusConfigUpdated = (cfg, version) => {
this.logger.debug('Received remote config update...');
const configInstance = configuration_js_1.Configuration.getInstance();
const config = configInstance.getConfig();
if (version > config.version) {
this.logger.debug(`Configuration has been updated from elsewhere to v${version}. Reloading...`);
return configInstance.updateConfigFromEvent(cfg, version, (err) => {
if (err) {
this.logger.error('Failed to update config from event', err);
}
});
}
};
const config = configuration_js_1.Configuration.getConfig();
this.logger = (0, redis_smq_common_1.createLogger)(config.logger, `${this.constructor.name}-${this.getId()}`);
}
setupEventListeners() {
const configInstance = configuration_js_1.Configuration.getInstance();
configInstance.on('configuration.updated', this.onLocalConfigUpdated);
internal_event_bus_js_1.InternalEventBus.getInstance().on('configuration.updated', this.onEventBusConfigUpdated);
}
cleanupEventListeners() {
const configInstance = configuration_js_1.Configuration.getInstance();
configInstance.removeListener('configuration.updated', this.onLocalConfigUpdated);
internal_event_bus_js_1.InternalEventBus.getInstance().removeListener('configuration.updated', this.onEventBusConfigUpdated);
}
goingUp() {
return super.goingUp().concat([
(cb) => {
this.setupEventListeners();
cb();
},
]);
}
goingDown() {
return [
(cb) => {
this.cleanupEventListeners();
cb();
},
].concat(super.goingDown());
}
static initialize(cb) {
if (!this.instance) {
this.instance = new ConfigSync();
return this.instance.run(cb);
}
cb();
}
static shutdown(cb) {
if (this.instance) {
return this.instance.shutdown(() => {
this.instance = null;
cb();
});
}
cb();
}
}
exports.ConfigSync = ConfigSync;
ConfigSync.instance = null;
//# sourceMappingURL=config-sync.js.map