diff --git a/loader.js b/loader.js index 7ec647bd22..0407c18302 100644 --- a/loader.js +++ b/loader.js @@ -132,9 +132,6 @@ Loader.addWorkerEvents = function(worker) { hash: message.hash }, worker.pid); break; - case 'config:update': - Loader.notifyWorkers(message); - break; } } }); diff --git a/src/meta/configs.js b/src/meta/configs.js index ae32d45599..e6d97f211b 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -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) { diff --git a/src/pubsub.js b/src/pubsub.js new file mode 100644 index 0000000000..42c0f54cfb --- /dev/null +++ b/src/pubsub.js @@ -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; \ No newline at end of file