Files
Ayay/SHH.CameraService/Core/ParentProcessSentinel.cs
twice109 3d47c8f009 增加了通过网络主动上报图像的支持
增加了指令维护通道的支持
2026-01-07 10:59:03 +08:00

79 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Diagnostics;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SHH.CameraSdk;
namespace SHH.CameraService;
public class ParentProcessSentinel : BackgroundService
{
private readonly ServiceConfig _config;
private readonly IHostApplicationLifetime _lifetime;
private readonly ILogger<ParentProcessSentinel> _logger;
public ParentProcessSentinel(
ServiceConfig config,
IHostApplicationLifetime lifetime,
ILogger<ParentProcessSentinel> logger)
{
_config = config;
_lifetime = lifetime;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
int pid = _config.ParentPid;
// 如果 PID 为 0 或负数,说明不需要守护(可能是手动启动调试)
if (pid <= 0)
{
_logger.LogInformation("未指定有效的父进程 PID守护模式已禁用。");
return;
}
_logger.LogInformation($"父进程守护已启动,正在监控 PID: {pid}");
while (!stoppingToken.IsCancellationRequested)
{
if (!IsParentRunning(pid))
{
_logger.LogWarning($"[ALERT] 检测到父进程 (PID:{pid}) 已退出!正在终止当前服务...");
// 触发程序优雅退出
_lifetime.StopApplication();
// 强制跳出循环
break;
}
// 每 2 秒检查一次,避免 CPU 浪费
await Task.Delay(2000, stoppingToken);
}
}
private bool IsParentRunning(int pid)
{
try
{
// 尝试获取进程对象
var process = Process.GetProcessById(pid);
// 检查是否已退出
if (process.HasExited) return false;
return true;
}
catch (ArgumentException)
{
// GetProcessById 在找不到 PID 时会抛出 ArgumentException
// 说明进程已经不存在了
return false;
}
catch (Exception ex)
{
_logger.LogError(ex, "检查父进程状态时发生未知错误,默认为存活");
return true; // 发生未知错误时,保守起见认为它还活着
}
}
}