Improve reload function
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run

This commit is contained in:
2dust 2025-01-14 14:49:17 +08:00
parent 780ccb1932
commit 5ae58e6a98
2 changed files with 21 additions and 28 deletions

View file

@ -67,13 +67,17 @@ namespace ServiceLib.Handler
return; return;
} }
UpdateFunc(true, $"{node.GetSummary()}"); UpdateFunc(false, $"{node.GetSummary()}");
UpdateFunc(false, $"{Utils.GetRuntimeInfo()}"); UpdateFunc(false, $"{Utils.GetRuntimeInfo()}");
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await CoreStop(); await CoreStop();
await Task.Delay(100); await Task.Delay(100);
await CoreStart(node); await CoreStart(node);
await CoreStartPreService(node); await CoreStartPreService(node);
if (_process != null)
{
UpdateFunc(true, $"{node.GetSummary()}");
}
} }
public async Task<int> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds) public async Task<int> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
@ -220,8 +224,6 @@ namespace ServiceLib.Handler
await RunProcessAsLinuxSudo(proc, fileName, coreInfo, configPath); await RunProcessAsLinuxSudo(proc, fileName, coreInfo, configPath);
} }
var startUpErrorMessage = new StringBuilder();
var startUpSuccessful = false;
if (displayLog) if (displayLog)
{ {
proc.OutputDataReceived += (sender, e) => proc.OutputDataReceived += (sender, e) =>
@ -233,11 +235,6 @@ namespace ServiceLib.Handler
{ {
if (Utils.IsNullOrEmpty(e.Data)) return; if (Utils.IsNullOrEmpty(e.Data)) return;
UpdateFunc(false, e.Data + Environment.NewLine); UpdateFunc(false, e.Data + Environment.NewLine);
if (!startUpSuccessful)
{
startUpErrorMessage.Append(e.Data + Environment.NewLine);
}
}; };
} }
proc.Start(); proc.Start();
@ -258,18 +255,12 @@ namespace ServiceLib.Handler
proc.BeginErrorReadLine(); proc.BeginErrorReadLine();
} }
await Task.Delay(500);
AppHandler.Instance.AddProcess(proc.Handle); AppHandler.Instance.AddProcess(proc.Handle);
if (proc.WaitForExit(1000)) if (proc is null or { HasExited: true })
{ {
proc.CancelErrorRead(); throw new Exception(ResUI.FailedToRunCore);
throw new Exception(displayLog ? startUpErrorMessage.ToString() : "启动进程失败并退出 (Failed to start the process and exited)");
} }
else
{
startUpSuccessful = true;
}
AppHandler.Instance.AddProcess(proc.Handle);
return proc; return proc;
} }
catch (Exception ex) catch (Exception ex)

View file

@ -222,6 +222,7 @@ namespace ServiceLib.ViewModels
await StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler); await StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
} }
BlReloadEnabled = true;
await Reload(); await Reload();
await AutoHideStartup(); await AutoHideStartup();
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu(); Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
@ -533,8 +534,17 @@ namespace ServiceLib.ViewModels
#region core job #region core job
public async Task Reload() public async Task Reload(int times = 0)
{ {
//If there are unfinished reload job, wait a few seconds and exec again.
if (!BlReloadEnabled)
{
await Task.Delay(3000);
if (times > 3) return;
await Reload(++times);
return;
}
BlReloadEnabled = false; BlReloadEnabled = false;
await LoadCore(); await LoadCore();
@ -558,16 +568,8 @@ namespace ServiceLib.ViewModels
private async Task LoadCore() private async Task LoadCore()
{ {
//if (_config.tunModeItem.enableTun) var node = await ConfigHandler.GetDefaultServer(_config);
//{ await CoreHandler.Instance.LoadCore(node);
// Task.Delay(1000).Wait();
// WindowsUtils.RemoveTunDevice();
//}
await Task.Run(async () =>
{
var node = await ConfigHandler.GetDefaultServer(_config);
await CoreHandler.Instance.LoadCore(node);
});
} }
public async Task CloseCore() public async Task CloseCore()