redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
157 lines • 5.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Base = void 0;
const uuid_1 = require("uuid");
const events_1 = require("events");
const events_2 = require("../common/events/events");
const configuration_1 = require("../config/configuration");
const redis_smq_common_1 = require("redis-smq-common");
class Base extends events_1.EventEmitter {
constructor(config = {}) {
super();
this.sharedRedisClient = null;
this.eventListeners = [];
this.setUpSharedRedisClient = (cb) => {
(0, redis_smq_common_1.createClientInstance)(this.config.redis, (err, client) => {
if (err)
cb(err);
else if (!client)
cb(new redis_smq_common_1.errors.EmptyCallbackReplyError());
else {
this.sharedRedisClient = client;
cb();
}
});
};
this.tearDownSharedRedisClient = (cb) => {
if (this.sharedRedisClient) {
this.sharedRedisClient.halt(() => {
this.sharedRedisClient = null;
cb();
});
}
else
cb();
};
this.tearDownEventListeners = (cb) => {
redis_smq_common_1.async.each(this.eventListeners, (listener, index, done) => listener.quit(done), (err) => {
if (err)
cb(err);
else {
this.eventListeners = [];
cb();
}
});
};
this.id = (0, uuid_1.v4)();
this.powerManager = new redis_smq_common_1.PowerManager(false);
this.config = (0, configuration_1.getConfiguration)(config);
this.logger = redis_smq_common_1.logger.getNamespacedLogger(this.config.logger, `${this.constructor.name.toLowerCase()}:${this.id}`);
this.registerSystemEventListeners();
}
registerSystemEventListeners() {
this.on(events_2.events.GOING_UP, () => this.logger.info(`Going up...`));
this.on(events_2.events.UP, () => this.logger.info(`Up and running...`));
this.on(events_2.events.GOING_DOWN, () => this.logger.info(`Going down...`));
this.on(events_2.events.DOWN, () => this.logger.info(`Down.`));
this.on(events_2.events.ERROR, (err) => this.handleError(err));
}
goingUp() {
return [this.setUpSharedRedisClient];
}
up(cb) {
this.powerManager.commit();
this.emit(events_2.events.UP);
cb && cb(null, true);
}
goingDown() {
return [this.tearDownEventListeners, this.tearDownSharedRedisClient];
}
down(cb) {
this.powerManager.commit();
this.emit(events_2.events.DOWN);
cb && cb(null, true);
}
getSharedRedisClient() {
if (!this.sharedRedisClient)
throw new redis_smq_common_1.errors.PanicError('Expected an instance of RedisClient');
return this.sharedRedisClient;
}
registerEventListeners(Ctors, cb) {
redis_smq_common_1.async.eachOf(Ctors, (ctor, key, done) => {
const instance = new ctor();
instance.init({
instanceId: this.id,
eventProvider: this,
config: this.getConfig(),
}, (err) => {
if (err)
done(err);
else {
this.eventListeners.push(instance);
done();
}
});
}, cb);
}
handleError(err) {
if (this.powerManager.isGoingUp() || this.powerManager.isRunning()) {
throw err;
}
}
run(cb) {
const r = this.powerManager.goingUp();
if (r) {
this.emit(events_2.events.GOING_UP);
const tasks = this.goingUp();
redis_smq_common_1.async.waterfall(tasks, (err) => {
if (err) {
if (cb)
cb(err);
else
this.emit(events_2.events.ERROR, err);
}
else
this.up(cb);
});
}
else {
cb && cb(null, r);
}
}
shutdown(cb) {
const r = this.powerManager.goingDown();
if (r) {
this.emit(events_2.events.GOING_DOWN);
const tasks = this.goingDown();
redis_smq_common_1.async.waterfall(tasks, () => {
this.down(cb);
});
}
else
cb && cb(null, r);
}
isRunning() {
return this.powerManager.isRunning();
}
isGoingUp() {
return this.powerManager.isGoingUp();
}
isGoingDown() {
return this.powerManager.isGoingDown();
}
isUp() {
return this.powerManager.isUp();
}
isDown() {
return this.powerManager.isDown();
}
getId() {
return this.id;
}
getConfig() {
return this.config;
}
}
exports.Base = Base;
//# sourceMappingURL=base.js.map