S5MdpRefreshJob.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Admin.NET.Core.Service;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform;
  3. using Admin.NET.Plugin.AiDOP.FinishedWarehouse;
  4. using Admin.NET.Plugin.AiDOP.Manufacturing;
  5. using Admin.NET.Plugin.AiDOP.MaterialWarehouse;
  6. using Furion.Schedule;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Logging;
  9. using System.Text.Json;
  10. namespace Admin.NET.Plugin.AiDOP.Job;
  11. /// <summary>
  12. /// T8 基础数据 + S5/S6/S7 KPI 统一刷新编排任务(方案④ owner)。
  13. /// 一个 tick 内:先执行 1 次 T8 基表双模式入站(源→mdp_stg_t8_*→mdp_std_t8_*),
  14. /// 再顺序计算 S5/S6/S7 三个模块的 T8 KPI(均读同一份本轮刷新后的 std,保证强一致)。
  15. /// JobId 保留 job_s5_t8_kpi_refresh(不迁移 Job 身份/历史);原 S6/S7 独立 Job 已合并至本 Job。
  16. /// 每日 02:00 / 07:00 / 12:00 / 17:00 / 22:00 共 5 次。
  17. ///
  18. /// 失败语义(关键,勿机械照抄 Bootstrap):
  19. /// ① T8 inbound 是整轮硬门——失败则记错误日志并立即结束本轮,
  20. /// 禁止继续算 S5/S6/S7(不允许退化为用上一周期 std 静默计算的 eventual consistency);
  21. /// ② inbound 成功后,S5→S6→S7 采用步级失败隔离(某模块失败记错误、继续下一模块);
  22. /// ③ OperationCanceledException 保持取消语义直接上抛,不吞。
  23. /// 手工补偿入口 /t8-base-mdp/inbound 仍独立保留。
  24. ///
  25. /// 并发:与手动刷新(AidopKanbanController → AidopT8KpiManualRefreshService)共享同一把硬 TTL 刷新锁
  26. /// (RefreshLockKey)。占用时本轮定时跳过(5 次/天,下一 tick 自愈),避免共享入站/std 与手动刷新并发写;
  27. /// 本轮结束在 finally 主动释放,即使异常/挂死也由 TTL 到点自动过期,不永久阻塞后续刷新。
  28. /// </summary>
  29. [JobDetail("job_s5_t8_kpi_refresh",
  30. Description = "T8 基础数据 + S5/S6/S7 KPI 统一刷新编排(inbound 硬门 → S5→S6→S7;5 次/天:02/07/12/17/22)",
  31. GroupName = "default",
  32. Concurrent = false)]
  33. [Cron("0 2,7,12,17,22 * * *",
  34. TriggerId = "trigger_s5_t8_kpi_refresh",
  35. Description = "每日 02:00/07:00/12:00/17:00/22:00 触发(5 字段:分 时 日 月 周,默认 CronStringFormat.Default)")]
  36. public class S5MdpRefreshJob : IJob
  37. {
  38. private readonly IServiceScopeFactory _scopeFactory;
  39. private readonly ILogger _logger;
  40. public S5MdpRefreshJob(IServiceScopeFactory scopeFactory, ILoggerFactory loggerFactory)
  41. {
  42. _scopeFactory = scopeFactory;
  43. _logger = loggerFactory.CreateLogger(nameof(S5MdpRefreshJob));
  44. }
  45. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  46. {
  47. // 与手动刷新共享同一把硬 TTL 刷新锁:占用时本轮跳过;本轮结束 finally 释放,异常/挂死由 TTL 自动过期兜底。
  48. using var lockScope = _scopeFactory.CreateScope();
  49. var cache = lockScope.ServiceProvider.GetRequiredService<SysCacheService>();
  50. if (cache.ExistKey(AidopT8KpiManualRefreshService.RefreshLockKey))
  51. {
  52. _logger.LogWarning("S5MdpRefreshJob 检测到刷新锁被占用(手动刷新或上轮未释放),本轮跳过");
  53. return;
  54. }
  55. cache.Set(AidopT8KpiManualRefreshService.RefreshLockKey, $"AUTO:{DateTime.Now:yyyyMMddHHmmss}",
  56. TimeSpan.FromSeconds(AidopT8KpiManualRefreshService.RefreshLockTtlSeconds));
  57. try
  58. {
  59. // ① 硬门:T8 基表入站(源→stg→std)必须先成功;失败则本轮结束,不继续下游、不降级用旧 std。
  60. try
  61. {
  62. using var inboundScope = _scopeFactory.CreateScope();
  63. var inbound = inboundScope.ServiceProvider.GetRequiredService<T8BaseInboundMdpSyncService>();
  64. var inboundResult = await inbound.RunInboundAsync(
  65. tenantId: 0, fullRefresh: true, entityCode: null, cancellationToken: stoppingToken);
  66. _logger.LogInformation("S5MdpRefreshJob T8 inbound 完成 {Payload}", JsonSerializer.Serialize(inboundResult));
  67. }
  68. catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
  69. {
  70. _logger.LogInformation("S5MdpRefreshJob T8 inbound 收到停止信号,本轮结束");
  71. throw;
  72. }
  73. catch (Exception ex)
  74. {
  75. _logger.LogError(ex, "S5MdpRefreshJob T8 inbound 失败,本轮跳过 S5/S6/S7 KPI 刷新(不使用上一周期 std 继续计算)");
  76. return;
  77. }
  78. // ② 下游:inbound 成功后,S5→S6→S7 顺序执行,步级失败隔离(各自独立 DI scope)。
  79. await RunStepAsync("S5", stoppingToken, async sp =>
  80. (object)await sp.GetRequiredService<S5MdpSyncTransformService>().RunFullAsync(stoppingToken, "AUTO"));
  81. await RunStepAsync("S6", stoppingToken, async sp =>
  82. (object)await sp.GetRequiredService<S6MdpSyncTransformService>().RunFullAsync(stoppingToken, "AUTO"));
  83. await RunStepAsync("S7", stoppingToken, async sp =>
  84. (object)await sp.GetRequiredService<S7MdpSyncTransformService>().RunFullAsync(stoppingToken, "AUTO"));
  85. }
  86. finally
  87. {
  88. cache.Remove(AidopT8KpiManualRefreshService.RefreshLockKey);
  89. }
  90. }
  91. /// <summary>
  92. /// 下游模块步级执行:独立 DI scope + 失败隔离(仿 SmartOpsKpiMdpBootstrapJob.RunStepAsync)。
  93. /// 仅用于 inbound 成功之后的 S5/S6/S7;不得用于包裹 inbound(inbound 失败必须中断本轮)。
  94. /// </summary>
  95. private async Task RunStepAsync(string step, CancellationToken stoppingToken, Func<IServiceProvider, Task<object>> action)
  96. {
  97. using var scope = _scopeFactory.CreateScope();
  98. try
  99. {
  100. stoppingToken.ThrowIfCancellationRequested();
  101. var payload = await action(scope.ServiceProvider);
  102. _logger.LogInformation("S5MdpRefreshJob {Step} 完成 {Payload}", step, JsonSerializer.Serialize(payload));
  103. }
  104. catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
  105. {
  106. _logger.LogInformation("S5MdpRefreshJob {Step} 收到停止信号", step);
  107. throw;
  108. }
  109. catch (Exception ex)
  110. {
  111. // 失败通知由各 service 内部 MarkTransformRunFailedAsync 触发;此处记录并继续下一步
  112. _logger.LogError(ex, "S5MdpRefreshJob {Step} 执行失败,继续后续步骤", step);
  113. }
  114. }
  115. }