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-357814968v1.18.x
parent
c20aca8933
commit
e85aabbe74
@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
var nconf = require('nconf');
|
||||
|
||||
module.exports = function (db, mongoModule) {
|
||||
var pubsub;
|
||||
|
||||
if (!nconf.get('redis')) {
|
||||
var mubsub = require('mubsub');
|
||||
var client = mubsub(db);
|
||||
pubsub = client.channel('pubsub');
|
||||
mongoModule.pubsub = pubsub;
|
||||
} else {
|
||||
pubsub = require('../../pubsub');
|
||||
}
|
||||
|
||||
pubsub.on('mongo:hash:cache:del', function (key) {
|
||||
mongoModule.objectCache.del(key);
|
||||
});
|
||||
|
||||
pubsub.on('mongo:hash:cache:reset', function () {
|
||||
mongoModule.objectCache.reset();
|
||||
});
|
||||
|
||||
mongoModule.delObjectCache = function (key) {
|
||||
pubsub.publish('mongo:hash:cache:del', key);
|
||||
mongoModule.objectCache.del(key);
|
||||
};
|
||||
|
||||
mongoModule.resetObjectCache = function () {
|
||||
pubsub.publish('mongo:hash:cache:reset');
|
||||
mongoModule.objectCache.reset();
|
||||
};
|
||||
};
|
||||
@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
var nconf = require('nconf');
|
||||
var util = require('util');
|
||||
var winston = require('winston');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var channelName;
|
||||
|
||||
var PubSub = function (redisModule) {
|
||||
var self = this;
|
||||
var subClient = redisModule.connect();
|
||||
this.pubClient = redisModule.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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
util.inherits(PubSub, EventEmitter);
|
||||
|
||||
PubSub.prototype.publish = function (event, data) {
|
||||
this.pubClient.publish(channelName, JSON.stringify({ event: event, data: data }));
|
||||
};
|
||||
|
||||
module.exports = function (redisClient, redisModule) {
|
||||
redisModule.pubsub = new PubSub(redisModule);
|
||||
};
|
||||
Loading…
Reference in New Issue