test pubsub

This commit is contained in:
Baris Usakli
2018-10-17 14:20:15 -04:00
parent b4d8b7cf38
commit 444fd70ba3
2 changed files with 31 additions and 14 deletions

View File

@@ -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;
},
};