luci: Fix restart service when pressed on Save&Apply or Restart buttons

This commit is contained in:
remittor
2026-01-27 13:52:28 +03:00
parent e0dbf3653d
commit 5860c9829a
4 changed files with 75 additions and 35 deletions

View File

@@ -50,6 +50,6 @@ return baseclass.extend({
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';
dst_obj.skey_deffered_action = this.appName + '-deffered-action';
}
});

View File

@@ -62,7 +62,9 @@ return view.extend({
});
},
setAppStatus: function(status_array, elems = { }, force_app_status = 0) {
setAppStatus: function(status_array, elems = { }, force_app_status = 0)
{
tools.execDefferedAction();
let cfg = uci.get(tools.appName, 'config');
if (!status_array || cfg == null || typeof(cfg) !== 'object') {
let elem_status = elems.status || document.getElementById("status");
@@ -141,6 +143,15 @@ return view.extend({
this.disableButtons(true, btn);
poll.stop();
try {
if (action == 'start' || action == 'restart') {
let apply_exec = tools.checkUnsavedChanges();
if (apply_exec) {
ui.changes.apply(true); // apply_rollback
await new Promise(resolve => setTimeout(resolve, 1000));
tools.setDefferedAction(action, null, true);
return;
}
}
await tools.serviceActionEx(action, args, false);
if (hide_modal) {
ui.hideModal();
@@ -148,13 +159,14 @@ return view.extend({
} catch(e) {
//ui.addNotification(null, E('p', 'Error: ' + e.message));
} finally {
if (btn && btn_dis) {
setTimeout(() => { btn.disabled = true; }, 0);
}
if (!poll.active()) {
await new Promise(resolve => setTimeout(resolve, 10));
poll.start();
}
setTimeout(() => {
if (btn && btn_dis) {
btn.disabled = true;
}
if (!poll.active()) {
poll.start();
}
}, 0);
}
},
@@ -285,8 +297,6 @@ 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

@@ -24,7 +24,7 @@ return view.extend({
return;
}
this.svc_info = data.svc_info;
tools.checkAndRestartSvc(this.svc_info);
tools.execDefferedAction(this.svc_info);
let m, s, o, tabname;
@@ -458,10 +458,14 @@ return view.extend({
handleSaveApply: function(ev, mode)
{
return this.handleSave(ev).then(() => {
ui.changes.apply(mode == '0');
if (this.svc_info?.dmn.inited) {
localStorage.setItem(tools.skey_need_restart, '1');
console.log('STOR KEY: '+tools.skey_need_restart+' = 1');
let apply_exec = tools.checkUnsavedChanges();
if (apply_exec) {
ui.changes.apply(mode == '0');
tools.setDefferedAction('restart', this.svc_info);
} else {
if (this.svc_info?.dmn.inited) {
tools.serviceActionEx('restart');
}
}
});
},

View File

@@ -160,10 +160,6 @@ return baseclass.extend({
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.');
}
@@ -198,7 +194,7 @@ return baseclass.extend({
uci.load(this.appName),
])
.then( ([svcInfo, uci_data]) => {
let svc_info = this.decode_svc_info(true, svcInfo, [ ], null);
let svc_info = this.decodeSvcInfo(svcInfo);
let ret = { svc_info, uci_data };
if (typeof callback === 'function') {
const res = callback(cbarg, ret);
@@ -215,23 +211,49 @@ return baseclass.extend({
});
},
checkAndRestartSvc: function(svcInfo)
decodeSvcInfo: function(svc_info, svc_autorun = true, proc_list = [ ])
{
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);
if (svc_info?.autorun !== undefined && svc_info?.dmn !== undefined) {
return svc_info;
}
//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');
if (svc_info != null && typeof(svc_info) == 'object') {
return this.decode_svc_info(svc_autorun, svc_info, proc_list);
}
return null;
},
setDefferedAction: function(action, svcInfo = null, forced = false)
{
let svc_info = this.decodeSvcInfo(svcInfo);
if (action == 'start' && svc_info?.dmn.inited) {
action = 'restart';
}
if (action == 'start') {
if (!forced && svc_info?.dmn.inited) {
action = null;
}
}
if (action == 'restart') {
if (!forced && !svc_info?.dmn.inited) {
action = null;
}
}
if (action && localStorage.getItem(this.skey_deffered_action) == null) {
localStorage.setItem(this.skey_deffered_action, action);
console.log('setDefferedAction: '+this.skey_deffered_action+' = '+action);
}
},
execDefferedAction: function(svcInfo = null)
{
let svc_info = this.decodeSvcInfo(svcInfo);
//console.log('execDefferedAction: svc_info = '+JSON.stringify(svc_info));
let action = localStorage.getItem(this.skey_deffered_action);
if (action) {
localStorage.removeItem(this.skey_deffered_action);
console.log('execDefferedAction: '+action);
this.serviceActionEx(action);
}
},
checkUnsavedChanges: function()
@@ -320,7 +342,8 @@ return baseclass.extend({
return plist;
},
decode_svc_info: function(svc_autorun, svc_info, proc_list, cfg) {
decode_svc_info: function(svc_autorun, svc_info, proc_list, cfg = null)
{
let result = {
"autorun": svc_autorun,
"dmn": {
@@ -341,6 +364,9 @@ return baseclass.extend({
return -3;
}
}
if (svc_info == null) {
return null;
}
if (typeof(svc_info) !== 'object') {
return -4;
}