From 9c996e4b9976e177a056fd9e428b2087a6235ded Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 29 Sep 2014 16:35:13 -0400 Subject: [PATCH] on config save update meta.config on all workers also dont make 20 socket.emit calls for each setting field, everything will be send in 1 socket call. --- loader.js | 5 +- public/src/forum/admin/settings.js | 105 ++++++++++++++--------------- src/meta/configs.js | 47 +++++++++++-- src/socket.io/admin.js | 29 +++++++- 4 files changed, 125 insertions(+), 61 deletions(-) diff --git a/loader.js b/loader.js index 777be064d8..63ad8a6977 100644 --- a/loader.js +++ b/loader.js @@ -76,7 +76,8 @@ Loader.init = function() { break; case 'user:connect': case 'user:disconnect': - notifyWorkers(worker, message); + case 'config:update': + notifyWorkers(message); break; } } @@ -87,7 +88,7 @@ Loader.init = function() { console.log('[cluster] Child Process (' + worker.process.pid + ') listening for connections.'); }); - function notifyWorkers(currentWorker, msg) { + function notifyWorkers(msg) { Object.keys(cluster.workers).forEach(function(id) { cluster.workers[id].send(msg); }); diff --git a/public/src/forum/admin/settings.js b/public/src/forum/admin/settings.js index 67a2401220..f1cd25ff14 100644 --- a/public/src/forum/admin/settings.js +++ b/public/src/forum/admin/settings.js @@ -61,38 +61,25 @@ define('forum/admin/settings', ['uploader', 'sounds'], function(uploader, sounds saveBtn.off('click').on('click', function(e) { e.preventDefault(); - var done = 0, - error; - for (x = 0; x < numFields; x++) { - saveField(fields[x], onFieldSaved); - } - - function onFieldSaved(err) { - if (!error && err) { - error = err; - } - - done++; - if (done === numFields) { - if (error) { - return app.alert({ - alert_id: 'config_status', - timeout: 2500, - title: 'Changes Not Saved', - message: 'NodeBB encountered a problem saving your changes', - type: 'danger' - }); - } - app.alert({ + saveFields(fields, function onFieldsSaved(err) { + if (err) { + return app.alert({ alert_id: 'config_status', timeout: 2500, - title: 'Changes Saved', - message: 'Your changes to the NodeBB configuration have been saved.', - type: 'success' + title: 'Changes Not Saved', + message: 'NodeBB encountered a problem saving your changes', + type: 'danger' }); } - } + app.alert({ + alert_id: 'config_status', + timeout: 2500, + title: 'Changes Saved', + message: 'Your changes to the NodeBB configuration have been saved.', + type: 'success' + }); + }); }); handleUploads(); @@ -131,38 +118,50 @@ define('forum/admin/settings', ['uploader', 'sounds'], function(uploader, sounds socket.emit('admin.config.remove', key); }; - function saveField(field, callback) { - field = $(field); - var key = field.attr('data-field'), - value, inputType; + function saveFields(fields, callback) { + var data = {}; - if (field.is('input')) { - inputType = field.attr('type'); - switch (inputType) { - case 'text': - case 'password': - case 'hidden': - case 'textarea': - case 'number': + fields.each(function() { + var field = $(this); + var key = field.attr('data-field'), + value, inputType; + + if (field.is('input')) { + inputType = field.attr('type'); + switch (inputType) { + case 'text': + case 'password': + case 'hidden': + case 'textarea': + case 'number': + value = field.val(); + break; + + case 'checkbox': + value = field.prop('checked') ? '1' : '0'; + break; + } + } else if (field.is('textarea') || field.is('select')) { value = field.val(); - break; + } + + data[key] = value; + }); - case 'checkbox': - value = field.prop('checked') ? '1' : '0'; - break; + socket.emit('admin.config.setMultiple', data, function(err) { + if (err) { + return callback(err); } - } else if (field.is('textarea') || field.is('select')) { - value = field.val(); - } - socket.emit('admin.config.set', { - key: key, - value: value - }, function(err) { - if(!err && app.config[key] !== undefined) { - app.config[key] = value; + if (app.config) { + for(var field in data) { + if (data.hasOwnProperty(field)) { + app.config[field] = data[field]; + } + } } - callback(err); + + callback(); }); } diff --git a/src/meta/configs.js b/src/meta/configs.js index 1a542e8e9c..39198f57ec 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -43,15 +43,54 @@ module.exports = function(Meta) { return callback(new Error('invalid config field')); } - db.setObjectField('config', field, value, function(err, res) { - if (!err && Meta.config) { - Meta.config[field] = value; + db.setObjectField('config', field, value, function(err) { + if (err) { + return callback(err); } + var data = {}; + data[field] = value; + updateConfig(data); - callback(err, res); + callback(); }); }; + Meta.configs.setMultiple = function(data, callback) { + db.setObject('config', data, function(err) { + if (err) { + return callback(err); + } + + updateConfig(data); + callback(); + }); + }; + + function updateConfig(data) { + var msg = {action: 'config:update', data: data}; + if (process.send) { + process.send(msg); + } else { + onMessage(msg); + } + } + + process.on('message', onMessage); + + function onMessage(msg) { + if (typeof msg !== 'object') { + 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]; + } + } + } + } + Meta.configs.setOnEmpty = function (field, value, callback) { Meta.configs.get(field, function (err, curValue) { if (err) { diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 21efa75ba7..17bc3d0e20 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -74,7 +74,7 @@ SocketAdmin.themes.set = function(socket, data, callback) { callback(); }); }; - if (data.type == 'bootswatch') { + if (data.type === 'bootswatch') { wrappedCallback(); } else { widgets.reset(wrappedCallback); @@ -126,6 +126,31 @@ SocketAdmin.config.set = function(socket, data, callback) { }); }; +SocketAdmin.config.setMultiple = function(socket, data, callback) { + if(!data) { + return callback(new Error('[[error:invalid-data]]')); + } + + meta.configs.setMultiple(data, function(err) { + if(err) { + return callback(err); + } + + callback(); + var setting; + for(var field in data) { + if (data.hasOwnProperty(field)) { + setting = { + key: field, + value: data[field] + }; + plugins.fireHook('action:config.set', setting); + logger.monitorConfig({io: index.server}, setting); + } + } + }); +}; + SocketAdmin.config.remove = function(socket, key) { meta.configs.remove(key); }; @@ -194,7 +219,7 @@ function getHourlyStatsForSet(set, hours, callback) { next(err); }); } - + }, function(err) { var termsArr = [];