redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
73 lines • 2.7 kB
JavaScript
import { createLogger, Runnable } from 'redis-smq-common';
import { Configuration } from './configuration.js';
import { EventMultiplexer } from '../event-bus/event-multiplexer.js';
import { InternalEventBus } from '../event-bus/internal-event-bus.js';
export class ConfigSync extends Runnable {
static instance = null;
logger;
constructor() {
super();
const config = Configuration.getConfig();
this.logger = createLogger(config.logger, `${this.constructor.name}-${this.getId()}`);
}
onLocalConfigUpdated = (cfg, version) => {
this.logger.debug('Publishing local config update...', cfg);
EventMultiplexer.getInstance().publish('configuration.updated', cfg, version);
};
onEventBusConfigUpdated = (cfg, version) => {
this.logger.debug('Received remote config update...');
const configInstance = 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);
}
});
}
};
setupEventListeners() {
const configInstance = Configuration.getInstance();
configInstance.on('configuration.updated', this.onLocalConfigUpdated);
InternalEventBus.getInstance().on('configuration.updated', this.onEventBusConfigUpdated);
}
cleanupEventListeners() {
const configInstance = Configuration.getInstance();
configInstance.removeListener('configuration.updated', this.onLocalConfigUpdated);
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();
}
}
//# sourceMappingURL=config-sync.js.map