S8TimeoutAutoEscalationJob.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Admin.NET.Plugin.AiDOP.Service.S8;
  2. using Furion.Schedule;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Logging;
  6. namespace Admin.NET.Plugin.AiDOP.Job;
  7. /// <summary>
  8. /// S8-TIMEOUT-AUTO-ESCALATION-JOB-1(P4-1):每 5 分钟扫描 sla_deadline 已超时的异常并触发 ApprovalFlow 升级。
  9. /// 实现位于 <see cref="S8TimeoutAutoEscalationService.RunOnceAsync"/>,便于管理员接口或测试复用。
  10. /// 与 <see cref="ApprovalFlow.FlowTimeoutJob"/> 同模式(IServiceScopeFactory + Furion Schedule)。
  11. /// </summary>
  12. [JobDetail("job_s8_timeout_auto_escalation",
  13. Description = "S8 超时自动升级(扫描 sla_deadline 已过期的异常并启动 EXCEPTION_ESCALATION)",
  14. GroupName = "default", Concurrent = false)]
  15. [Period(300000, TriggerId = "trigger_s8_timeout_auto_escalation", Description = "每5分钟执行")]
  16. public class S8TimeoutAutoEscalationJob : IJob
  17. {
  18. private readonly IServiceScopeFactory _scopeFactory;
  19. private readonly IConfiguration _configuration;
  20. private readonly ILogger _logger;
  21. // S8-SCHEDULER-P0-BLEEDING-STOP-CONFIG-1:禁用日志单进程内只输出一次,避免每 tick 刷日志。
  22. // _firstParseFailLogged 控制 AIDOP_* env 解析失败 warn 单进程内只输出一次(首个失败 env 名)。
  23. private static int _firstDisabledLogged;
  24. private static int _firstParseFailLogged;
  25. // S8-SCHEDULER-CONFIG-ORDER-FIX(合入 P0):AIDOP_* env override 名称。
  26. // Furion 4.9.8.24 JSON 装配链后置导致 ASP.NET Core env vars 被 JSON 覆盖,
  27. // 故此处用 Environment.GetEnvironmentVariable 做 S8 调度开关专项 override。
  28. private const string EnvSchedulerEnabled = "AIDOP_SCHEDULER_ENABLED";
  29. private const string EnvS8SchedulerEnabled = "AIDOP_S8_SCHEDULER_ENABLED";
  30. public S8TimeoutAutoEscalationJob(IServiceScopeFactory scopeFactory, IConfiguration configuration, ILoggerFactory loggerFactory)
  31. {
  32. _scopeFactory = scopeFactory;
  33. _configuration = configuration;
  34. _logger = loggerFactory.CreateLogger("S8TimeoutAutoEscalationJob");
  35. }
  36. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  37. {
  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 schedulerEnabled = _configuration.GetValue("Scheduler:Enabled", true);
  43. var s8SchedulerEnabled = _configuration.GetValue("S8:Scheduler:Enabled", false);
  44. string? parseFailEnvName = null;
  45. var envSchedulerRaw = Environment.GetEnvironmentVariable(EnvSchedulerEnabled);
  46. if (!string.IsNullOrWhiteSpace(envSchedulerRaw))
  47. {
  48. if (bool.TryParse(envSchedulerRaw, out var v)) schedulerEnabled = v;
  49. else parseFailEnvName ??= EnvSchedulerEnabled;
  50. }
  51. var envS8SchedulerRaw = Environment.GetEnvironmentVariable(EnvS8SchedulerEnabled);
  52. if (!string.IsNullOrWhiteSpace(envS8SchedulerRaw))
  53. {
  54. if (bool.TryParse(envS8SchedulerRaw, out var v)) s8SchedulerEnabled = v;
  55. else parseFailEnvName ??= EnvS8SchedulerEnabled;
  56. }
  57. if (parseFailEnvName != null && Interlocked.Exchange(ref _firstParseFailLogged, 1) == 0)
  58. {
  59. _logger.LogWarning(
  60. "S8TimeoutAutoEscalationJob env override 解析失败:{EnvName},已沿用配置值",
  61. parseFailEnvName);
  62. }
  63. if (!schedulerEnabled || !s8SchedulerEnabled)
  64. {
  65. if (Interlocked.Exchange(ref _firstDisabledLogged, 1) == 0)
  66. {
  67. _logger.LogInformation(
  68. "S8TimeoutAutoEscalationJob 被配置禁用:Scheduler:Enabled={Scheduler} S8:Scheduler:Enabled={S8Scheduler}",
  69. schedulerEnabled, s8SchedulerEnabled);
  70. }
  71. return;
  72. }
  73. using var scope = _scopeFactory.CreateScope();
  74. var svc = scope.ServiceProvider.GetRequiredService<S8TimeoutAutoEscalationService>();
  75. try
  76. {
  77. var processed = await svc.RunOnceAsync(50, stoppingToken);
  78. if (processed > 0)
  79. _logger.LogInformation("S8TimeoutAutoEscalationJob 本轮升级了 {Count} 个超时异常", processed);
  80. }
  81. catch (Exception ex)
  82. {
  83. _logger.LogError(ex, "S8TimeoutAutoEscalationJob 执行失败");
  84. }
  85. }
  86. }