using Admin.NET.Plugin.AiDOP.Service.S8;
using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
using Furion.Schedule;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Admin.NET.Plugin.AiDOP.Job;
///
/// S8-TIMEOUT-AUTO-ESCALATION-JOB-1(P4-1):每 5 分钟扫描 sla_deadline 已超时的异常并触发 ApprovalFlow 升级。
/// 实现位于 ,便于管理员接口或测试复用。
/// 与 同模式(IServiceScopeFactory + Furion Schedule)。
///
[JobDetail("job_s8_timeout_auto_escalation",
Description = "S8 超时自动升级(扫描 sla_deadline 已过期的异常并启动 EXCEPTION_ESCALATION)",
GroupName = "default", Concurrent = false)]
[Period(300000, TriggerId = "trigger_s8_timeout_auto_escalation", Description = "每5分钟执行")]
public class S8TimeoutAutoEscalationJob : IJob
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly IConfiguration _configuration;
private readonly ILogger _logger;
// S8-SCHEDULER-P0-BLEEDING-STOP-CONFIG-1:禁用日志单进程内只输出一次,避免每 tick 刷日志。
// _firstParseFailLogged 控制 AIDOP_* env 解析失败 warn 单进程内只输出一次(首个失败 env 名)。
private static int _firstDisabledLogged;
private static int _firstParseFailLogged;
// S8-SCHEDULER-CONFIG-ORDER-FIX(合入 P0):AIDOP_* env override 名称。
// Furion 4.9.8.24 JSON 装配链后置导致 ASP.NET Core env vars 被 JSON 覆盖,
// 故此处用 Environment.GetEnvironmentVariable 做 S8 调度开关专项 override。
public S8TimeoutAutoEscalationJob(IServiceScopeFactory scopeFactory, IConfiguration configuration, ILoggerFactory loggerFactory)
{
_scopeFactory = scopeFactory;
_configuration = configuration;
_logger = loggerFactory.CreateLogger("S8TimeoutAutoEscalationJob");
}
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
{
// S8-SCHEDULER-P0-BLEEDING-STOP-CONFIG-1:环境级 + S8 业务级双开关 gate。
// 任一为 false 时直接早退;早退前不访问任何业务 service / DB。
// env override(合入 CONFIG-ORDER-FIX):env 不存在/空白 → 沿用 JSON;
// 存在但 bool.TryParse 失败 → 保留 JSON 值 + 单次 warn(首个失败 env 名,不输出 value)。
var gate = S8JobSwitch.Evaluate(_configuration, S8BackgroundJob.TimeoutAutoEscalation);
if (gate.ParseFailEnvName != null && Interlocked.Exchange(ref _firstParseFailLogged, 1) == 0)
{
_logger.LogWarning(
"S8TimeoutAutoEscalationJob env override 解析失败:{EnvName},已沿用配置值",
gate.ParseFailEnvName);
}
if (!gate.Enabled)
{
if (Interlocked.Exchange(ref _firstDisabledLogged, 1) == 0)
{
_logger.LogInformation(
"S8TimeoutAutoEscalationJob 被配置禁用:Scheduler:Enabled={Scheduler} S8:Scheduler:Enabled={S8Scheduler} {JobKey}={JobEnabled}",
gate.EnvironmentEnabled, gate.MasterEnabled,
S8JobSwitch.JobEnabledKey(S8BackgroundJob.TimeoutAutoEscalation), gate.JobEnabled);
}
return;
}
using var scope = _scopeFactory.CreateScope();
var svc = scope.ServiceProvider.GetRequiredService();
try
{
var processed = await svc.RunOnceAsync(50, stoppingToken);
if (processed > 0)
_logger.LogInformation("S8TimeoutAutoEscalationJob 本轮升级了 {Count} 个超时异常", processed);
}
catch (Exception ex)
{
_logger.LogError(ex, "S8TimeoutAutoEscalationJob 执行失败");
}
}
}