105 lines
4.1 KiB
C#
105 lines
4.1 KiB
C#
#if false
|
||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||
using OrpaonVision.SiteApp.Runtime.Services;
|
||
|
||
namespace OrpaonVision.SiteApp.Runtime.HealthChecks;
|
||
|
||
/// <summary>
|
||
/// 状态机健康检查。
|
||
/// </summary>
|
||
public sealed class StateMachineHealthCheck : IHealthCheck
|
||
{
|
||
private readonly IRuntimeStateMachineService _stateMachineService;
|
||
private readonly ILogger<StateMachineHealthCheck> _logger;
|
||
|
||
public StateMachineHealthCheck(IRuntimeStateMachineService stateMachineService, ILogger<StateMachineHealthCheck> logger)
|
||
{
|
||
_stateMachineService = stateMachineService;
|
||
_logger = logger;
|
||
}
|
||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||
{
|
||
try
|
||
{
|
||
// 检查状态机当前状态
|
||
var currentState = _stateMachineService.GetCurrentState();
|
||
var currentLayer = _stateMachineService.GetCurrentLayer();
|
||
|
||
// 检查状态机是否响应
|
||
var canInitialize = _stateMachineService.CanExecuteOperation(Core.Common.StateTrigger.Initialize);
|
||
var canStart = _stateMachineService.CanExecuteOperation(Core.Common.StateTrigger.Start);
|
||
|
||
// 获取事件历史
|
||
var historyResult = await _stateMachineService.GetEventHistoryAsync(10, cancellationToken);
|
||
|
||
if (!historyResult.IsSuccess)
|
||
{
|
||
return HealthCheckResult.Unhealthy(
|
||
"状态机事件历史获取失败",
|
||
new Dictionary<string, object>
|
||
{
|
||
["error"] = historyResult.Message,
|
||
["trace_id"] = historyResult.TraceId ?? string.Empty
|
||
});
|
||
}
|
||
|
||
var eventCount = historyResult.Data?.Count ?? 0;
|
||
var lastEventTime = eventCount > 0 ? historyResult.Data?.FirstOrDefault()?.TimestampUtc : (DateTime?)null;
|
||
|
||
// 判断健康状态
|
||
var data = new Dictionary<string, object>
|
||
{
|
||
["current_state"] = currentState.ToString(),
|
||
["current_layer"] = currentLayer,
|
||
["can_initialize"] = canInitialize,
|
||
["can_start"] = canStart,
|
||
["event_count"] = eventCount,
|
||
["last_event_time"] = lastEventTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "无事件",
|
||
["status"] = "healthy"
|
||
};
|
||
|
||
// 检查是否处于异常状态
|
||
if (currentState == Core.Common.RuntimeState.Error || currentState == Core.Common.RuntimeState.Faulted)
|
||
{
|
||
return HealthCheckResult.Unhealthy(
|
||
"状态机处于异常状态",
|
||
new Dictionary<string, object>(data)
|
||
{
|
||
["status"] = "error_state"
|
||
});
|
||
}
|
||
|
||
// 检查是否长时间无活动(超过5分钟)
|
||
if (lastEventTime.HasValue && DateTime.UtcNow.Subtract(lastEventTime.Value).TotalMinutes > 5)
|
||
{
|
||
return HealthCheckResult.Degraded(
|
||
"状态机长时间无活动",
|
||
new Dictionary<string, object>(data)
|
||
{
|
||
["status"] = "inactive",
|
||
["inactive_minutes"] = DateTime.UtcNow.Subtract(lastEventTime.Value).TotalMinutes
|
||
});
|
||
}
|
||
|
||
_logger.LogDebug("状态机健康检查通过:当前状态={CurrentState},当前层级={CurrentLayer},事件数量={EventCount}",
|
||
currentState, currentLayer, eventCount);
|
||
|
||
return HealthCheckResult.Healthy("状态机运行正常", data);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "状态机健康检查异常");
|
||
return HealthCheckResult.Unhealthy(
|
||
"状态机健康检查异常",
|
||
new Dictionary<string, object>
|
||
{
|
||
["error"] = ex.Message,
|
||
["exception_type"] = ex.GetType().Name
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
#endif
|