S8TimeoutAutoEscalationJob.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // S8-SCHEDULER-P0-BLEEDING-STOP-CONFIG-1:环境级 + S8 业务级双开关 gate。
  38. // 任一为 false 时直接早退;早退前不访问任何业务 service / DB。
  39. // env override(合入 CONFIG-ORDER-FIX):env 不存在/空白 → 沿用 JSON;
  40. // 存在但 bool.TryParse 失败 → 保留 JSON 值 + 单次 warn(首个失败 env 名,不输出 value)。
  41. var gate = S8JobSwitch.Evaluate(_configuration, S8BackgroundJob.TimeoutAutoEscalation);
  42. if (gate.ParseFailEnvName != null && Interlocked.Exchange(ref _firstParseFailLogged, 1) == 0)
  43. {
  44. _logger.LogWarning(
  45. "S8TimeoutAutoEscalationJob env override 解析失败:{EnvName},已沿用配置值",
  46. gate.ParseFailEnvName);
  47. }
  48. if (!gate.Enabled)
  49. {
  50. if (Interlocked.Exchange(ref _firstDisabledLogged, 1) == 0)
  51. {
  52. _logger.LogInformation(
  53. "S8TimeoutAutoEscalationJob 被配置禁用:Scheduler:Enabled={Scheduler} S8:Scheduler:Enabled={S8Scheduler} {JobKey}={JobEnabled}",
  54. gate.EnvironmentEnabled, gate.MasterEnabled,
  55. S8JobSwitch.JobEnabledKey(S8BackgroundJob.TimeoutAutoEscalation), gate.JobEnabled);
  56. }
  57. return;
  58. }
  59. using var scope = _scopeFactory.CreateScope();
  60. var svc = scope.ServiceProvider.GetRequiredService<S8TimeoutAutoEscalationService>();
  61. try
  62. {
  63. var processed = await svc.RunOnceAsync(50, stoppingToken);
  64. if (processed > 0)
  65. _logger.LogInformation("S8TimeoutAutoEscalationJob 本轮升级了 {Count} 个超时异常", processed);
  66. }
  67. catch (Exception ex)
  68. {
  69. _logger.LogError(ex, "S8TimeoutAutoEscalationJob 执行失败");
  70. }
  71. }
  72. }