diff --git a/XCoder/Program.cs b/XCoder/Program.cs index 28057c5..bd1eba0 100644 --- a/XCoder/Program.cs +++ b/XCoder/Program.cs @@ -1,9 +1,11 @@ using System.Diagnostics; +using System.Net.NetworkInformation; using System.Text; using NewLife; using NewLife.Log; using NewLife.Threading; using Stardust; +using Stardust.Models; using Stardust.Services; namespace XCoder; @@ -45,8 +47,6 @@ static class Program XTrace.WriteLine("初始化服务端地址:{0}", server); - var star = new StarFactory(); - var client = new StarClient(server) { Code = set.Code, @@ -67,6 +67,7 @@ static class Program } }; + // 使用跟踪 client.UseTrace(); Application.ApplicationExit += (s, e) => client.Logout("ApplicationExit"); @@ -79,24 +80,50 @@ static class Program private static async Task TryConnectServer(Object state) { + if (!NetworkInterface.GetIsNetworkAvailable() || AgentInfo.GetIps().IsNullOrEmpty()) + { + return; + } + var client = state as StarClient; - var set = XConfig.Current; - await client.Login(); - await CheckUpgrade(client, set.Channel); - // 登录成功,销毁定时器 - //TimerX.Current.Period = 0; + try + { + await client.Login(); + //await CheckUpgrade(client); + } + catch (Exception ex) + { + // 登录报错后,加大定时间隔,输出简单日志 + //_timer.Period = 30_000; + if (_timer.Period < 30_000) _timer.Period += 5_000; + + XTrace.Log?.Error(ex.Message); + + return; + } + _timer.TryDispose(); - _timer = null; + _timer = new TimerX(CheckUpgrade, null, 5_000, 600_000) { Async = true }; + + client.RegisterCommand("node/upgrade", s => _timer.SetNext(-1)); } private static String _lastVersion; - private static async Task CheckUpgrade(StarClient client, String channel) + private static async Task CheckUpgrade(Object data) { + var client = _Client; + using var span = client.Tracer?.NewSpan("CheckUpgrade", new { _lastVersion }); + + // 运行过程中可能改变配置文件的通道 + var set = XConfig.Current; var ug = new Stardust.Web.Upgrade { Log = XTrace.Log }; + // 去除多余入口文件 + ug.Trim("CrazyCoder"); + // 检查更新 - var ur = await client.Upgrade(channel); + var ur = await client.Upgrade(set.Channel); if (ur != null && ur.Version != _lastVersion) { client.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{ur.Version}],开始下载 {ur.Source}"); @@ -134,26 +161,26 @@ static class Program if (rs && !ur.Executor.IsNullOrEmpty()) ug.Run(ur.Executor); _lastVersion = ur.Version; + // 去除多余入口文件 + ug.Trim("CrazyCoder"); + // 强制更新时,马上重启 if (rs && ur.Force) { // 重新拉起进程 - var star = "CrazyCoder.exe"; - XTrace.WriteLine("强制升级,拉起进程 {0} -upgrade", star.GetFullPath()); - var p = Process.Start(star.GetFullPath(), "-upgrade"); + rs = ug.Run("CrazyCoder.exe", "-run -upgrade"); - if (p.WaitForExit(5_000) && p.ExitCode != 0) + if (rs) { - client.WriteInfoEvent("Upgrade", "强制更新完成,但拉起新进程失败"); + var pid = Process.GetCurrentProcess().Id; + client.WriteInfoEvent("Upgrade", "强制更新完成,新进程已拉起,准备退出当前进程!PID=" + pid); + + ug.KillSelf(); } else { - client.WriteInfoEvent("Upgrade", "强制更新完成,新进程已拉起,准备退出当前进程"); - - ug.KillSelf(); + client.WriteInfoEvent("Upgrade", "强制更新完成,但拉起新进程失败"); } - - Application.Exit(); } } }