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 hash: message.hash
}, worker.pid); }, worker.pid);
break; break;
case 'config:update':
Loader.notifyWorkers(message);
break;
} }
} }
}); });

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