From 66a0314bb906c4e2eb3b0dc1c9a04960a8c63827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 22 Oct 2024 22:34:53 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=E6=98=9F=E5=B0=98=E4=BB=A3=E7=90=86StarA?= =?UTF-8?q?gent=E5=9C=A8windows=E7=B3=BB=E7=BB=9F=E4=B8=AD=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BB=A5=E6=A1=8C=E9=9D=A2=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=BA=AB=E4=BB=BD=E8=BF=90=E8=A1=8C=E7=A8=8B=E5=BA=8F=EF=BC=8C?= =?UTF-8?q?=E5=8F=AA=E9=9C=80=E6=8A=8A=E7=94=A8=E6=88=B7=E5=90=8D=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=BA$?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StarAgent/StarAgent.csproj | 9 ++++++++ Stardust/Managers/ServiceController.cs | 29 ++++++++++++++++++++------ Stardust/Managers/ServiceManager.cs | 3 ++- Stardust/Stardust.csproj | 2 ++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/StarAgent/StarAgent.csproj b/StarAgent/StarAgent.csproj index 3a5ee631..d3218555 100644 --- a/StarAgent/StarAgent.csproj +++ b/StarAgent/StarAgent.csproj @@ -36,6 +36,11 @@ + + + + + @@ -49,4 +54,8 @@ + + + + diff --git a/Stardust/Managers/ServiceController.cs b/Stardust/Managers/ServiceController.cs index e1cb5e77..f2766aad 100644 --- a/Stardust/Managers/ServiceController.cs +++ b/Stardust/Managers/ServiceController.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using NewLife; +using NewLife.Agent.Windows; using NewLife.Log; using NewLife.Remoting.Clients; using NewLife.Threading; @@ -393,7 +394,19 @@ public Boolean Start() if (!si.UserName.IsNullOrEmpty()) WriteLog("启动用户:{0}", si.UserName); - var p = Process.Start(si); + Process? p = null; + + // Windows桌面用户运行 + if (Runtime.Windows && service.UserName == "$") + { + var desktop = new Desktop { Log = Log }; + var pid = desktop.StartProcess(si.FileName, si.Arguments, si.WorkingDirectory); + p = Process.GetProcessById((Int32)pid); + } + else + { + p = Process.Start(si); + } if (StartWait > 0 && p != null && p.WaitForExit(StartWait) && p.ExitCode != 0) { WriteLog("启动失败!ExitCode={0}", p.ExitCode); @@ -605,20 +618,24 @@ public Boolean Check() // 检查内存限制 if (inf.MaxMemory <= 0) return p; + var mem = p.WorkingSet64 / 1024 / 1024; + span?.AppendTag($"MaxMemory={inf.MaxMemory}M WorkingSet64={mem}M"); + // 定期清理内存 - if (Runtime.Windows && _nextCollect < DateTime.Now) + if (Runtime.Windows && _nextCollect < DateTime.Now && mem > inf.MaxMemory) { _nextCollect = DateTime.Now.AddSeconds(600); try { - NativeMethods.EmptyWorkingSet(p.Handle); + Runtime.FreeMemory(p.Id); + //NativeMethods.EmptyWorkingSet(p.Handle); } catch { } - } - var mem = p.WorkingSet64 / 1024 / 1024; - span?.AppendTag($"MaxMemory={inf.MaxMemory}M WorkingSet64={mem}M"); + p.Refresh(); + mem = p.WorkingSet64 / 1024 / 1024; + } if (mem <= inf.MaxMemory) return p; WriteLog("内存超限!{0}>{1}", mem, inf.MaxMemory); diff --git a/Stardust/Managers/ServiceManager.cs b/Stardust/Managers/ServiceManager.cs index 6d4ec2cc..0c54260e 100644 --- a/Stardust/Managers/ServiceManager.cs +++ b/Stardust/Managers/ServiceManager.cs @@ -1,4 +1,5 @@ -using NewLife; +using System.Net.Http; +using NewLife; using NewLife.Http; using NewLife.IO; using NewLife.Log; diff --git a/Stardust/Stardust.csproj b/Stardust/Stardust.csproj index 45098f1b..8ee56e6c 100644 --- a/Stardust/Stardust.csproj +++ b/Stardust/Stardust.csproj @@ -72,6 +72,8 @@ + +