mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-06-17 01:50:47 +02:00
sync config changes over redis pubsub
This commit is contained in:
@@ -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) {
|
||||
|
||||
45
src/pubsub.js
Normal file
45
src/pubsub.js
Normal file
@@ -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;
|
||||
Reference in New Issue
Block a user