/// <summary> /// 启动当前管道 /// </summary> /// <typeparam name="TEvent"></typeparam> /// <param name="input"></param> /// <returns></returns> public async Task Start <TEvent>(TEvent input) where TEvent : EventProcessBase { //初始化状态 processCache = new EventProcessPiplineStateModel(); //当前处理器 ProcessConfigureBase currentProcess = default; //当前事件 dynamic currentEvent = input; while (true) { switch (processCache.ProcessState) { case ProcessStateEnum.Normal: if (GetProcessee().TryDequeue(out ProcessConfigureBase process)) { await process.Excute(processCache, currentEvent); processCache.AutoResetEvent.WaitOne(); //若未进行任何操作且调用链并未执行完毕,则会自动触发RollBack。 if (processCache.NextEvent == null) { if (processCache.ProcessState != ProcessStateEnum.RollBack) { if (GetProcessee().TryPeek(out _)) { processCache.ProcessState = ProcessStateEnum.RollBack; } else { processCache.ProcessState = ProcessStateEnum.End; } } else { _ = processCache.PopRollBackParam(); //由于本次执行失败,强制弹出本次插入的数据 } } else { currentEvent = processCache.NextEvent; currentProcess = process; processCache.SetRollbackParams(currentEvent); } } else { processCache.ProcessState = ProcessStateEnum.End; } break; case ProcessStateEnum.RollBack: if (GetProcessCallbackList(currentProcess).TryPop(out ProcessConfigureBase callback)) { currentEvent = processCache.PopRollBackParam(); await callback.Excute(processCache, currentEvent); processCache.AutoResetEvent.WaitOne(); //只要没有触发RollBackError且回滚列表未回滚完毕,则继续执行RollBack if (processCache.ProcessState != ProcessStateEnum.RollBackError) { processCache.ProcessState = ProcessStateEnum.RollBack; } } else { processCache.ProcessState = ProcessStateEnum.RollBackEnd; } break; case ProcessStateEnum.End: //流程结束 logger.LogInformation("流程执行成功,打印结束日志!"); return; case ProcessStateEnum.RollBackEnd: //流程结束 logger.LogWarning("流程执行失败,回滚全部成功,打印结束日志!"); return; case ProcessStateEnum.RollBackError: //流程结束 logger.LogError("流程执行失败,回滚异常,打印结束日志!"); return; } } }
public abstract Task Excute <TEvent>(EventProcessPiplineStateModel processCache, TEvent input) where TEvent : EventProcessBase;
/// <summary> /// 发送事件代理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="processCache"></param> /// <param name="input"></param> /// <returns></returns> public override async Task Excute <T>(EventProcessPiplineStateModel processCache, T input) { await eventBus.Send(topic, new EventProcessPacker <T>() { processCache = processCache, Input = input }); }