sync config changes over redis pubsub

v1.18.x
barisusakli 10 years ago
parent 2f8e6a1f2b
commit 936ea5f686

@ -132,9 +132,6 @@ Loader.addWorkerEvents = function(worker) {
hash: message.hash
}, worker.pid);
break;
case 'config:update':
Loader.notifyWorkers(message);
break;
}
}
});

@ -3,6 +3,7 @@
var winston = require('winston'),
db = require('../database'),
pubsub = require('../pubsub'),
pkg = require('../../package.json');
module.exports = function(Meta) {
@ -92,34 +93,25 @@ module.exports = function(Meta) {
return callback(null, '');
}
data.renderedCustomCSS = lessObject.css;
callback(null, lessObject.css);
callback();
});
}
function updateConfig(data) {
var msg = {action: 'config:update', data: data};
if (process.send) {
process.send(msg);
} else {
onMessage(msg);
}
function updateConfig(config) {
pubsub.publish('config:update', config);
}
process.on('message', onMessage);
function onMessage(msg) {
if (typeof msg !== 'object') {
pubsub.on('config:update', function onConfigReceived(config) {
if (typeof config !== 'object' || !Meta.config) {
return;
}
if (msg.action === 'config:update' && Meta.config) {
for(var field in msg.data) {
if(msg.data.hasOwnProperty(field)) {
Meta.config[field] = msg.data[field];
}
for(var field in config) {
if(config.hasOwnProperty(field)) {
Meta.config[field] = config[field];
}
}
}
});
Meta.configs.setOnEmpty = function (field, value, callback) {
Meta.configs.get(field, function (err, curValue) {

@ -0,0 +1,45 @@
'use strict';
var nconf = require('nconf'),
util = require('util'),
winston = require('winston'),
EventEmitter = require('events').EventEmitter;
var PubSub = function() {
var self = this;
if (nconf.get('redis')) {
var redis = require('./database/redis');
var subClient = redis.connect();
this.pubClient = redis.connect();
subClient.subscribe('pubsub_channel');
subClient.on('message', function(channel, message) {
if (channel !== 'pubsub_channel') {
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) {
if (this.pubClient) {
this.pubClient.publish('pubsub_channel', JSON.stringify({event: event, data: data}));
} else {
this.emit(event, data);
}
};
var pubsub = new PubSub();
module.exports = pubsub;
Loading…
Cancel
Save