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.
v1.18.x
barisusakli 10 years ago
parent 219cd9661f
commit 9c996e4b99

@ -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);
});

@ -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();
});
}

@ -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) {

@ -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 = [];

Loading…
Cancel
Save