luci: Restart service on press Save&Apply

This commit is contained in:
remittor
2026-01-26 22:05:22 +03:00
parent c6dfbc5c7e
commit df196ef9a9
4 changed files with 119 additions and 75 deletions

View File

@@ -49,5 +49,7 @@ return baseclass.extend({
dst_obj.packager.path = '/bin/opkg';
dst_obj.packager.args = [ 'list-installed', '*'+this.appName+'*' ];
}
dst_obj.skey_pkg_dict = this.appName + '-pkg-dict';
dst_obj.skey_need_restart = this.appName + '-need-restart';
}
});

View File

@@ -73,7 +73,7 @@ return view.extend({
}
let svc_boot = status_array[0] ? true : false;
let svc_en = status_array[1]; // stdout: empty or error text
let svc_info = status_array[2]; // stdout: JSON as text
let svc_info = status_array[2]; // dict for services
let proc_list = status_array[3]; // stdout: multiline text
let pkg_dict = status_array[4]; // stdout: installed packages
let stratlist = status_array[5]; // array of strat names
@@ -140,45 +140,21 @@ return view.extend({
let btn = document.getElementById(button);
this.disableButtons(true, btn);
poll.stop();
let errmsg = null;
try {
let exec_cmd = null;
let exec_arg = [ ];
if (action == 'start' || action == 'restart') {
if (tools.checkUnsavedChanges()) {
ui.changes.apply(true);
await new Promise(resolve => setTimeout(resolve, 100));
}
exec_cmd = tools.syncCfgPath;
errmsg = _('Unable to run sync_config.sh script.');
}
if (action == 'reset') {
exec_cmd = tools.defaultCfgPath;
exec_arg = args; // (reset_ipset)(sync) ==> restore all configs + sync config
errmsg = _('Unable to run restore-def-cfg.sh script.');
action = null;
}
if (exec_cmd) {
let res = await fs.exec(exec_cmd, exec_arg);
if (res.code != 0) {
throw Error('res.code = ' + res.code);
}
}
await tools.serviceActionEx(action, args, false);
if (hide_modal) {
ui.hideModal();
}
errmsg = null;
await tools.handleServiceAction(tools.appName, action);
} catch(e) {
let msg = errmsg ? errmsg : _('Unable to run service action') + ' "' + action + '".';
ui.addNotification(null, E('p', msg + ' Error: ' + e.message));
//ui.addNotification(null, E('p', 'Error: ' + e.message));
} finally {
if (!poll.active()) {
poll.start();
}
if (btn && btn_dis) {
setTimeout(() => { btn.disabled = true; }, 0);
}
if (!poll.active()) {
await new Promise(resolve => setTimeout(resolve, 10));
poll.start();
}
}
},
@@ -309,6 +285,8 @@ return view.extend({
}
let cfg = uci.get(tools.appName, 'config');
tools.checkAndRestartSvc(status_array[2]); // svc_info
let pkgdict = status_array[4];
if (pkgdict == null) {
ui.addNotification(null, E('p', _('Unable to enumerate installed packages') + ': render()'));

View File

@@ -13,43 +13,18 @@ document.head.appendChild(E('link', {
}));
return view.extend({
parsers: { },
appStatusCode: null,
depends: function(elem, key, array, empty=true) {
if (empty && array.length === 0) {
elem.depends(key, '_dummy');
} else {
array.forEach(e => elem.depends(key, e));
}
},
validateIpPort: function(section, value) {
return (/^$|^([0-9]{1,3}\.){3}[0-9]{1,3}(#[\d]{2,5})?$/.test(value)) ? true : _('Expecting:')
+ ` ${_('One of the following:')}\n - ${_('valid IP address')}\n - ${_('valid address#port')}\n`;
},
validateUrl: function(section, value) {
return (/^$|^https?:\/\/[\w.-]+(:[0-9]{2,5})?[\w\/~.&?+=-]*$/.test(value)) ? true : _('Expecting:')
+ ` ${_('valid URL')}\n`;
},
svc_info: null,
load: function() {
return Promise.all([
{ code: -1}, // L.resolveDefault(fs.exec(tools.execPath, [ 'raw-status' ]), 1),
null, // L.resolveDefault(fs.list(tools.parsersDir), null),
uci.load(tools.appName),
]).catch(e => {
ui.addNotification(null, E('p', _('Unable to read the contents') + ': %s '.format(e.message) ));
});
return tools.baseLoad();
},
render: function(data) {
if (!data) {
return;
}
this.appStatusCode = data[0].code;
this.svc_info = data.svc_info;
tools.checkAndRestartSvc(this.svc_info);
let m, s, o, tabname;
@@ -480,12 +455,14 @@ return view.extend({
return map_promise;
},
handleSaveApply: function(ev, mode) {
handleSaveApply: function(ev, mode)
{
return this.handleSave(ev).then(() => {
ui.changes.apply(mode == '0');
//if (this.appStatusCode != 1 && this.appStatusCode != 2) {
// window.setTimeout(() => fs.exec(tools.execPath, [ 'restart' ]), 3000);
//}
if (this.svc_info?.dmn.inited) {
localStorage.setItem(tools.skey_need_restart, '1');
console.log('STOR KEY: '+tools.skey_need_restart+' = 1');
}
});
},
});

View File

@@ -103,11 +103,10 @@ return baseclass.extend({
getPackageDict: function()
{
let ses_var_name = this.appName+'_pkgdict';
let exec_cmd = this.packager.path;
let exec_arg = this.packager.args;
return fs.exec(exec_cmd, exec_arg).then(res => {
let pdict_json = sessionStorage.getItem(ses_var_name);
let pdict_json = localStorage.getItem(this.skey_pkg_dict);
if (res.code != 0) {
console.log(this.appName + ': Unable to enumerate installed packages. code = ' + res.code);
if (pdict_json != null) {
@@ -117,7 +116,7 @@ return baseclass.extend({
}
let pdict = this.decode_pkg_list(res.stdout);
if (pdict != pdict_json) {
sessionStorage.setItem(ses_var_name, JSON.stringify(pdict)); // renew cache
localStorage.setItem(this.skey_pkg_dict, JSON.stringify(pdict)); // renew cache
}
return pdict;
}).catch(e => {
@@ -138,7 +137,9 @@ return baseclass.extend({
});
},
handleServiceAction: function(name, action) {
handleServiceAction: function(name, action, throwed = false)
{
console.log('handleServiceAction: '+name+' '+action);
return this.callInitAction(name, action).then(success => {
if (!success) {
throw _('Command failed');
@@ -146,9 +147,93 @@ return baseclass.extend({
return true;
}).catch(e => {
ui.addNotification(null, E('p', _('Service action failed "%s %s": %s').format(name, action, e)));
if (throwed) {
throw e;
}
});
},
serviceActionEx: async function(action, args = [ ], throwed = false)
{
let errmsg = null;
try {
let exec_cmd = null;
let exec_arg = [ ];
if (action == 'start' || action == 'restart') {
if (this.checkUnsavedChanges()) {
await ui.changes.apply(true);
await new Promise(resolve => setTimeout(resolve, 100));
}
exec_cmd = this.syncCfgPath;
errmsg = _('Unable to run sync_config.sh script.');
}
if (action == 'reset') {
exec_cmd = this.defaultCfgPath;
exec_arg = args; // (reset_ipset)(sync) ==> restore all configs + sync config
errmsg = _('Unable to run restore-def-cfg.sh script.');
action = null;
}
if (exec_cmd) {
let res = await fs.exec(exec_cmd, exec_arg);
if (res.code != 0) {
throw Error('res.code = ' + res.code);
}
}
errmsg = null;
await this.handleServiceAction(this.appName, action, throwed);
} catch(e) {
if (throwed) {
throw e;
} else {
let msg = errmsg ? errmsg : _('Unable to run service action') + ' "' + action + '".';
ui.addNotification(null, E('p', msg + ' Error: ' + e.message));
}
}
},
baseLoad: function(callback, cbarg)
{
return Promise.all([
this.getSvcInfo(), // svc_info
uci.load(this.appName),
])
.then( ([svcInfo, uci_data]) => {
let svc_info = this.decode_svc_info(true, svcInfo, [ ], null);
let ret = { svc_info, uci_data };
if (typeof callback === 'function') {
const res = callback(cbarg, ret);
if (res && typeof res.then === 'function') {
return res.then(() => ret);
}
return ret;
}
return ret;
})
.catch(e => {
ui.addNotification(null, E('p', _('Unable to read the contents') + ' (baseLoad): %s '.format(e.message) ));
return null;
});
},
checkAndRestartSvc: function(svcInfo)
{
let svc_info = null;
if (svcInfo?.autorun !== undefined && svcInfo?.dmn !== undefined) {
svc_info = svcInfo;
} else
if (typeof(svcInfo) == 'object') {
svc_info = this.decode_svc_info(true, svcInfo, [ ], null);
}
//console.log('checkAndRestartSvc: svc_info = '+JSON.stringify(svc_info));
let need_restart = localStorage.getItem(this.skey_need_restart);
if (need_restart) {
localStorage.removeItem(this.skey_need_restart);
if (svcInfo?.dmn !== undefined && svc_info.dmn.inited) {
this.serviceActionEx('restart');
}
}
},
checkUnsavedChanges: function()
{
if (!ui.changes) return false;
@@ -246,13 +331,15 @@ return baseclass.extend({
},
"status": this.statusDict.error,
};
if (proc_list.code != 0) {
return -2;
}
let plist = this.get_pid_list(proc_list.stdout);
if (plist.length < 4) {
return -3;
let plist = proc_list;
if (proc_list?.code !== undefined) {
if (proc_list.code != 0) {
return -2;
}
plist = this.get_pid_list(proc_list.stdout);
if (plist.length < 4) {
return -3;
}
}
if (typeof(svc_info) !== 'object') {
return -4;