using MediatR;
using Microsoft.Extensions.Logging;
using X1.Application.Features.TaskExecution.Events.Interfaces;
using X1.Application.Features.TaskExecution.Events.NodeExecutionEvents;
using X1.Domain.Events;
using X1.Domain.Entities.TestCase;
namespace X1.Application.Features.TaskExecution.Events.ControllerHandlers;
///
/// 结束流程控制器处理器
/// 处理测试流程结束相关的节点执行事件
///
public class EndFlowControllerHandler : INodeExecutionHandlerBase
{
///
/// 初始化结束流程控制器处理器
///
/// MediatR 中介者
/// 日志记录器
public EndFlowControllerHandler(IMediator mediator, ILogger logger)
: base(mediator, logger)
{
}
///
/// 处理结束流程节点执行事件
///
/// 节点执行事件
/// 取消令牌
/// 处理任务
public override async Task HandleAsync(ControllerExecutionEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("开始执行结束流程,任务执行ID: {TaskExecutionId}, 节点ID: {NodeId}, 运行时编码: {RuntimeCode}",
notification.TaskExecutionId, notification.NodeId, notification.RuntimeCode);
try
{
// 1. 更新节点状态为运行中
await UpdateNodeStatusAsync(notification.NodeId, NodeExecutionStatus.Running);
// 2. 执行结束流程业务逻辑
var result = await ExecuteEndFlowAsync(notification, cancellationToken);
// 3. 创建执行结果
var executionResult = new NodeExecutionResult
{
TaskExecutionId = notification.TaskExecutionId,
NodeId = notification.NodeId,
StepMapping = notification.StepMapping,
Status = NodeExecutionStatus.Completed,
IsSuccess = true,
ResultData = result,
ExecutorId = notification.ExecutorId,
RuntimeCode = notification.RuntimeCode,
CreatedAt = DateTime.UtcNow
};
// 4. 发布完成事件
await PublishCompletedEventAsync(notification, executionResult, cancellationToken);
_logger.LogInformation("结束流程执行成功,任务执行ID: {TaskExecutionId}, 节点ID: {NodeId}",
notification.TaskExecutionId, notification.NodeId);
}
catch (Exception ex)
{
_logger.LogError(ex, "结束流程执行失败,任务执行ID: {TaskExecutionId}, 节点ID: {NodeId}",
notification.TaskExecutionId, notification.NodeId);
// 发布失败事件
await PublishFailedEventAsync(notification, ex.Message, cancellationToken);
}
}
///
/// 检查是否支持处理指定的事件类型
///
/// 步骤映射类型
/// 是否支持
public override bool CanHandle(StepMapping stepMapping)
{
return stepMapping == StepMapping.EndFlow;
}
///
/// 获取处理器支持的步骤映射类型
///
/// 支持的步骤映射类型
public override StepMapping GetSupportedStepMapping()
{
return StepMapping.EndFlow;
}
///
/// 执行结束流程业务逻辑
///
/// 事件通知
/// 取消令牌
/// 执行结果
private async Task ExecuteEndFlowAsync(ControllerExecutionEvent notification, CancellationToken cancellationToken)
{
// TODO: 实现具体的结束流程逻辑
// 这里应该调用实际的结束流程服务
_logger.LogInformation("正在执行结束流程,运行时编码: {RuntimeCode}", notification.RuntimeCode);
// 模拟异步操作
await Task.Delay(300, cancellationToken);
// 模拟成功结果
var result = new
{
Status = "Success",
Message = "测试流程结束成功",
Timestamp = DateTime.UtcNow,
RuntimeCode = notification.RuntimeCode,
FlowCompleted = true,
ResourcesCleaned = true
};
return System.Text.Json.JsonSerializer.Serialize(result);
}
///
/// 更新节点状态
///
/// 节点ID
/// 新状态
/// 更新任务
private async Task UpdateNodeStatusAsync(string nodeId, NodeExecutionStatus status)
{
// TODO: 实现节点状态更新逻辑
// 这里应该更新数据库中的节点状态
_logger.LogInformation("更新节点状态,节点ID: {NodeId}, 新状态: {Status}", nodeId, status);
// 模拟异步操作
await Task.CompletedTask;
}
}