Skip to content

Commit

Permalink
本地应用看门狗测试通过
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 2, 2023
1 parent 05e9d24 commit 6885f0e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion StarAgent/StarAgent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.Agent" Version="10.6.2023.1016-beta1607" />
<PackageReference Include="NewLife.Agent" Version="10.6.2023.1102" />
</ItemGroup>

<ItemGroup>
Expand Down
44 changes: 34 additions & 10 deletions StarAgent/StarService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
using NewLife;
using NewLife.Agent;
using NewLife.Log;
Expand Down Expand Up @@ -94,6 +96,10 @@ public AgentInfo Info(AgentInfo info)
return ai;
}

/// <summary>本地心跳</summary>
/// <param name="info"></param>
/// <returns></returns>
[Api(nameof(Ping))]
public PingResponse Ping(LocalPingInfo info)
{
if (info != null && info.ProcessId > 0)
Expand Down Expand Up @@ -261,14 +267,16 @@ private void CheckLocal()
#region 看门狗
static ConcurrentDictionary<Int32, DateTime> _dogs = new();
static TimerX _dogTimer;
static void FeedDog(Int32 pid, Int32 timeout)
void FeedDog(Int32 pid, Int32 timeout)
{
if (pid <= 0 || timeout <= 0) return;

using var span = DefaultTracer.Instance?.NewSpan(nameof(FeedDog), new { pid, timeout });

// 更新过期时间,超过该时间未收到心跳,将会重启本应用进程
_dogs[pid] = DateTime.Now.AddSeconds(timeout);

_dogTimer ??= new TimerX(CheckDog, null, 1000, 15000) { Async = true };
_dogTimer ??= new TimerX(CheckDog, Manager, 1000, 15000) { Async = true };
}

static void CheckDog(Object state)
Expand All @@ -287,20 +295,33 @@ static void CheckDog(Object state)
}
}

if (ks.Count == 0 && ds.Count == 0) return;

using var span = DefaultTracer.Instance?.NewSpan(nameof(CheckDog));

// 重启超时的进程
foreach (var item in ks)
{
var p = Process.GetProcessById(item);
if (p == null || p.HasExited)
_dogs.Remove(item);
else
try
{
XTrace.WriteLine("进程[{0}/{1}]超过一定时间没有心跳,可能已经假死,准备重启。", p.ProcessName, p.Id);
var p = Process.GetProcessById(item);
if (p == null || p.HasExited)
_dogs.Remove(item);
else
{
XTrace.WriteLine("进程[{0}/{1}]超过一定时间没有心跳,可能已经假死,准备重启。", p.ProcessName, p.Id);

p.SafetyKill();
span?.AppendTag($"SafetyKill {p.ProcessName}/{p.Id}");
p.SafetyKill();

//todo 启动应用。暂时不需要,因为StarAgent会自动启动
if (state is ServiceManager manager) manager.CheckNow();
//todo 启动应用。暂时不需要,因为StarAgent会自动启动
if (state is ServiceManager manager) manager.CheckNow();
}
}
catch (Exception ex)
{
_dogs.Remove(item);
XTrace.WriteException(ex);
}
}

Expand All @@ -313,6 +334,9 @@ static void CheckDog(Object state)
#endregion

#region 日志
/// <summary>链路追踪</summary>
public ITracer Tracer { get; set; }

/// <summary>日志</summary>
public ILog Log { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions Stardust/Managers/ServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public Boolean Check()
// 遍历所有进程,从命令行参数中找到启动文件名一致的进程
foreach (var item in Process.GetProcesses())
{
if (item.Id == mypid) continue;
if (item.Id == mypid || item.HasExited) continue;
if (!item.ProcessName.EqualIgnoreCase(ProcessName)) continue;

var name = AppInfo.GetProcessName(item);
Expand All @@ -575,7 +575,7 @@ public Boolean Check()
{
span?.AppendTag($"GetProcessesByName({ProcessName})");

var ps = Process.GetProcessesByName(ProcessName).Where(e => e.Id != mypid).ToArray();
var ps = Process.GetProcessesByName(ProcessName).Where(e => e.Id != mypid && !e.HasExited).ToArray();
if (ps.Length > 0) return TakeOver(ps[0], $"按[Name={ProcessName}]查找");
}
}
Expand All @@ -587,7 +587,7 @@ public Boolean Check()
p = Process;
if (p != null && EventProvider is StarClient client)
{
if (_appInfo == null)
if (_appInfo == null || _appInfo.Id != p.Id)
_appInfo = new AppInfo(p) { AppName = inf.Name };
else
_appInfo.Refresh();
Expand Down
2 changes: 1 addition & 1 deletion Stardust/Stardust.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

<ItemGroup Condition="'$(TargetFramework)'!='net40'">
<PackageReference Include="NewLife.Core" Version="10.6.2023.1101" />
<PackageReference Include="NewLife.Remoting" Version="2.5.2023.1101" />
<PackageReference Include="NewLife.Remoting" Version="2.5.2023.1102" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net40'">
<PackageReference Include="NewLife.Core" Version="10.6.2023.1101-net40" />
Expand Down
13 changes: 13 additions & 0 deletions Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Stardust;
using Stardust.Data;
using Stardust.Data.Nodes;
using Stardust.Models;
using Stardust.Monitors;

namespace Test;
Expand Down Expand Up @@ -130,6 +131,18 @@ static void Test3()
//client.ProbeAndInstall(null, "1.6");
var info = client.GetInfo();

var appInfo = new AppInfo(Process.GetCurrentProcess());

for (var i = 0; i < 5; i++)
{
_ = client.PingAsync(appInfo, 5);

Thread.Sleep(2000);
}

Console.WriteLine("等待");
Console.ReadLine();

//var p = Process.GetCurrentProcess();
//var name = p.MainModule.FileName;
//var str = name + Environment.NewLine + name.ToJson();
Expand Down

0 comments on commit 6885f0e

Please sign in to comment.