Skip to content

Commit

Permalink
[feat]星尘代理StarAgent在windows系统中,支持以桌面用户身份运行程序,只需把用户名设置为$
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 22, 2024
1 parent 05282cb commit 66a0314
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
9 changes: 9 additions & 0 deletions StarAgent/StarAgent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<ItemGroup>
<Compile Remove="StarService2.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Stardust\Managers\ServiceController.cs" Link="Managers\ServiceController.cs" />
<Compile Include="..\Stardust\Managers\ServiceManager.cs" Link="Managers\ServiceManager.cs" />
</ItemGroup>

<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />
Expand All @@ -49,4 +54,8 @@
<ProjectReference Include="..\Stardust\Stardust.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Managers\" />
</ItemGroup>

</Project>
29 changes: 23 additions & 6 deletions Stardust/Managers/ServiceController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using NewLife;
using NewLife.Agent.Windows;
using NewLife.Log;
using NewLife.Remoting.Clients;
using NewLife.Threading;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Stardust/Managers/ServiceManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NewLife;
using System.Net.Http;
using NewLife;
using NewLife.Http;
using NewLife.IO;
using NewLife.Log;
Expand Down
2 changes: 2 additions & 0 deletions Stardust/Stardust.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
</ItemGroup>

<ItemGroup>
<Compile Remove="Managers\ServiceController.cs" />
<Compile Remove="Managers\ServiceManager.cs" />
<Compile Remove="Models\CommandEventArgs.cs" />
<Compile Remove="Models\CommandInModel.cs" />
<Compile Remove="Models\CommandModel.cs" />
Expand Down

0 comments on commit 66a0314

Please sign in to comment.