mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-11 16:35:47 +01:00
test pubsub
This commit is contained in:
@@ -4,6 +4,8 @@ var EventEmitter = require('events');
|
||||
var nconf = require('nconf');
|
||||
|
||||
var real;
|
||||
var noCluster;
|
||||
var singleHost;
|
||||
|
||||
function get() {
|
||||
if (real) {
|
||||
@@ -13,14 +15,23 @@ function get() {
|
||||
var pubsub;
|
||||
|
||||
if (nconf.get('isCluster') === 'false') {
|
||||
pubsub = new EventEmitter();
|
||||
pubsub.publish = pubsub.emit.bind(pubsub);
|
||||
if (noCluster) {
|
||||
real = noCluster;
|
||||
return real;
|
||||
}
|
||||
noCluster = new EventEmitter();
|
||||
noCluster.publish = noCluster.emit.bind(noCluster);
|
||||
pubsub = noCluster;
|
||||
} else if (nconf.get('singleHostCluster')) {
|
||||
pubsub = new EventEmitter();
|
||||
if (singleHost) {
|
||||
real = singleHost;
|
||||
return real;
|
||||
}
|
||||
singleHost = new EventEmitter();
|
||||
if (!process.send) {
|
||||
pubsub.publish = pubsub.emit.bind(pubsub);
|
||||
singleHost.publish = singleHost.emit.bind(singleHost);
|
||||
} else {
|
||||
pubsub.publish = function (event, data) {
|
||||
singleHost.publish = function (event, data) {
|
||||
process.send({
|
||||
action: 'pubsub',
|
||||
event: event,
|
||||
@@ -29,10 +40,11 @@ function get() {
|
||||
};
|
||||
process.on('message', function (message) {
|
||||
if (message && typeof message === 'object' && message.action === 'pubsub') {
|
||||
pubsub.emit(message.event, message.data);
|
||||
singleHost.emit(message.event, message.data);
|
||||
}
|
||||
});
|
||||
}
|
||||
pubsub = singleHost;
|
||||
} else if (nconf.get('redis')) {
|
||||
pubsub = require('./database/redis/pubsub');
|
||||
} else if (nconf.get('mongo')) {
|
||||
@@ -55,4 +67,7 @@ module.exports = {
|
||||
removeAllListeners: function (event) {
|
||||
get().removeAllListeners(event);
|
||||
},
|
||||
reset: function () {
|
||||
real = null;
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user