S8TimeoutAutoEscalationJob.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Admin.NET.Plugin.AiDOP.Service.S8;
  2. using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
  3. using Furion.Schedule;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. namespace Admin.NET.Plugin.AiDOP.Job;
  8. /// <summary>
  9. /// S8-TIMEOUT-AUTO-ESCALATION-JOB-1(P4-1):每 5 分钟扫描 sla_deadline 已超时的异常并触发 ApprovalFlow 升级。
  10. /// 实现位于 <see cref="S8TimeoutAutoEscalationService.RunOnceAsync"/>,便于管理员接口或测试复用。
  11. /// 与 <see cref="ApprovalFlow.FlowTimeoutJob"/> 同模式(IServiceScopeFactory + Furion Schedule)。
  12. /// </summary>
  13. [JobDetail("job_s8_timeout_auto_escalation",
  14. Description = "S8 超时自动升级(扫描 sla_deadline 已过期的异常并启动 EXCEPTION_ESCALATION)",
  15. GroupName = "default", Concurrent = false)]
  16. [Period(300000, TriggerId = "trigger_s8_timeout_auto_escalation", Description = "每5分钟执行")]
  17. public class S8TimeoutAutoEscalationJob : IJob
  18. {
  19. private readonly IServiceScopeFactory _scopeFactory;
  20. private readonly IConfiguration _configuration;
  21. private readonly ILogger _logger;
  22. // S8-SCHEDULER-P0-BLEEDING-STOP-CONFIG-1:禁用日志单进程内只输出一次,避免每 tick 刷日志。
  23. // _firstParseFailLogged 控制 AIDOP_* env 解析失败 warn 单进程内只输出一次(首个失败 env 名)。
  24. private static int _firstDisabledLogged;
  25. private static int _firstParseFailLogged;
  26. // S8-SCHEDULER-CONFIG-ORDER-FIX(合入 P0):AIDOP_* env override 名称。
  27. // Furion 4.9.8.24 JSON 装配链后置导致 ASP.NET Core env vars 被 JSON 覆盖,
  28. // 故此处用 Environment.GetEnvironmentVariable 做 S8 调度开关专项 override。
  29. public S8TimeoutAutoEscalationJob(IServiceScopeFactory scopeFactory, IConfiguration configuration, ILoggerFactory loggerFactory)
  30. {
  31. _scopeFactory = scopeFactory;
  32. _configuration = configuration;
  33. _logger = loggerFactory.CreateLogger("S8TimeoutAutoEscalationJob");
  34. }
  35. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  36. {
  37. if (!AidopJobGate.ShouldRun(nameof(S8TimeoutAutoEscalationJob), _logger)) return;
  38. // S8-SCHEDULER-P0-BLEEDING-STOP-CONFIG-1:环境级 + S8 业务级双开关 gate。
  39. // 任一为 false 时直接早退;早退前不访问任何业务 service / DB。
  40. // env override(合入 CONFIG-ORDER-FIX):env 不存在/空白 → 沿用 JSON;
  41. // 存在但 bool.TryParse 失败 → 保留 JSON 值 + 单次 warn(首个失败 env 名,不输出 value)。
  42. var gate = S8JobSwitch.Evaluate(_configuration, S8BackgroundJob.TimeoutAutoEscalation);
  43. if (gate.ParseFailEnvName != null && Interlocked.Exchange(ref _firstParseFailLogged, 1) == 0)
  44. {
  45. _logger.LogWarning(
  46. "S8TimeoutAutoEscalationJob env override 解析失败:{EnvName},已沿用配置值",
  47. gate.ParseFailEnvName);
  48. }
  49. if (!gate.Enabled)
  50. {
  51. if (Interlocked.Exchange(ref _firstDisabledLogged, 1) == 0)
  52. {
  53. _logger.LogInformation(
  54. "S8TimeoutAutoEscalationJob 被配置禁用:Scheduler:Enabled={Scheduler} S8:Scheduler:Enabled={S8Scheduler} {JobKey}={JobEnabled}",
  55. gate.EnvironmentEnabled, gate.MasterEnabled,
  56. S8JobSwitch.JobEnabledKey(S8BackgroundJob.TimeoutAutoEscalation), gate.JobEnabled);
  57. }
  58. return;
  59. }
  60. using var scope = _scopeFactory.CreateScope();
  61. var svc = scope.ServiceProvider.GetRequiredService<S8TimeoutAutoEscalationService>();
  62. try
  63. {
  64. var processed = await svc.RunOnceAsync(50, stoppingToken);
  65. if (processed > 0)
  66. _logger.LogInformation("S8TimeoutAutoEscalationJob 本轮升级了 {Count} 个超时异常", processed);
  67. }
  68. catch (Exception ex)
  69. {
  70. _logger.LogError(ex, "S8TimeoutAutoEscalationJob 执行失败");
  71. }
  72. }
  73. }