From 9e3a759646f4999fcee60220daf29de3fc42acd1 Mon Sep 17 00:00:00 2001 From: haoliang <821644@qq.com> Date: Sun, 3 May 2026 10:08:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=AA=E8=A1=A8=E7=9B=98?= =?UTF-8?q?=E9=87=87=E9=9B=86=E6=9C=8D=E5=8A=A1=E7=8A=B6=E6=80=81=E5=88=A4?= =?UTF-8?q?=E6=96=AD=EF=BC=9A=E5=A2=9E=E5=8A=A0=E5=BF=83=E8=B7=B3=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原来只检查心跳表最后一条记录的status字段是否为running, 没有判断心跳是否已过期(服务停止后旧心跳数据仍在)。 现在增加90秒超时判断(3个心跳间隔),超时则显示已停止。 --- src/CncService/Impl/DashboardService.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/CncService/Impl/DashboardService.cs b/src/CncService/Impl/DashboardService.cs index 9258443..0402ff5 100644 --- a/src/CncService/Impl/DashboardService.cs +++ b/src/CncService/Impl/DashboardService.cs @@ -73,8 +73,26 @@ namespace CncService.Impl public object GetCollectorStatus() { var latest = _collectorHeartbeatRepository.GetLatest("collector-service"); - bool isRunning = latest != null && latest.Status == "running"; - long uptimeSeconds = isRunning ? (latest.UptimeSeconds ?? 0) : 0; + + // 心跳超时阈值:90秒(3个心跳间隔,采集服务默认每30秒上报一次) + const int heartbeatTimeoutSeconds = 90; + + bool isRunning = false; + long uptimeSeconds = 0; + + if (latest != null && latest.Status == "running") + { + // 检查最后心跳时间是否在阈值内,超时则判定为已停止 + var lastHeartbeat = latest.CreatedAt; + var elapsed = (DateTime.Now - lastHeartbeat).TotalSeconds; + isRunning = elapsed <= heartbeatTimeoutSeconds; + + if (isRunning) + { + uptimeSeconds = latest.UptimeSeconds ?? 0; + } + } + return new { status = isRunning ? "running" : "stopped", uptimeSeconds, lastCollectTime = latest?.LastCollectTime }; } }