Allow running as a cluster without Redis (#6233)

* [database/*] Allow databases other than Redis to provide pubsub for clustering if Redis is not present

* [pubsub] Delay messages sent before the database is ready until the database is ready.

* [pubsub] Restore old behavior of not using the database in non-clustered NodeBB instances.

See comment: https://github.com/NodeBB/NodeBB/pull/6233#issuecomment-357814968
This commit is contained in:
Ben Lubar
2018-01-18 12:02:56 -06:00
committed by Barış Soner Uşaklı
parent c20aca8933
commit e85aabbe74
8 changed files with 165 additions and 80 deletions

View File

@@ -1,48 +1,70 @@
'use strict';
var nconf = require('nconf');
var util = require('util');
var winston = require('winston');
var EventEmitter = require('events').EventEmitter;
var channelName;
var PubSub = function () {
var self = this;
if (nconf.get('redis')) {
var redis = require('./database/redis');
var subClient = redis.connect();
this.pubClient = redis.connect();
channelName = 'db:' + nconf.get('redis:database') + 'pubsub_channel';
subClient.subscribe(channelName);
subClient.on('message', function (channel, message) {
if (channel !== channelName) {
return;
}
try {
var msg = JSON.parse(message);
self.emit(msg.event, msg.data);
} catch (err) {
winston.error(err.stack);
}
});
}
var real;
var fake = {
publishQueue: [],
publish: function (event, data) {
fake.publishQueue.push({ event: event, data: data });
},
listenQueue: {},
on: function (event, callback) {
if (!Object.prototype.hasOwnProperty.call(fake.listenQueue, event)) {
fake.listenQueue[event] = [];
}
fake.listenQueue[event].push(callback);
},
removeAllListeners: function (event) {
delete fake.listenQueue[event];
},
};
util.inherits(PubSub, EventEmitter);
function get() {
if (real) {
return real;
}
PubSub.prototype.publish = function (event, data) {
if (this.pubClient) {
this.pubClient.publish(channelName, JSON.stringify({ event: event, data: data }));
var pubsub;
if (nconf.get('isCluster') === 'false') {
var EventEmitter = require('events');
pubsub = new EventEmitter();
pubsub.publish = pubsub.emit.bind(pubsub);
} else if (nconf.get('redis')) {
pubsub = require('./database/redis').pubsub;
} else {
this.emit(event, data);
pubsub = require('./database').pubsub;
}
if (!pubsub) {
return fake;
}
Object.keys(fake.listenQueue).forEach(function (event) {
fake.listenQueue[event].forEach(function (callback) {
pubsub.on(event, callback);
});
});
fake.publishQueue.forEach(function (msg) {
pubsub.publish(msg.event, msg.data);
});
real = pubsub;
fake = null;
return pubsub;
}
module.exports = {
publish: function (event, data) {
get().publish(event, data);
},
on: function (event, callback) {
get().on(event, callback);
},
removeAllListeners: function (event) {
get().removeAllListeners(event);
},
};
var pubsub = new PubSub();
module.exports = pubsub;