using Admin.NET.Plugin.AiDOP.Service.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。
private const string EnvSchedulerEnabled = "AIDOP_SCHEDULER_ENABLED";
private const string EnvS8SchedulerEnabled = "AIDOP_S8_SCHEDULER_ENABLED";
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 schedulerEnabled = _configuration.GetValue("Scheduler:Enabled", true);
var s8SchedulerEnabled = _configuration.GetValue("S8:Scheduler:Enabled", false);
string? parseFailEnvName = null;
var envSchedulerRaw = Environment.GetEnvironmentVariable(EnvSchedulerEnabled);
if (!string.IsNullOrWhiteSpace(envSchedulerRaw))
{
if (bool.TryParse(envSchedulerRaw, out var v)) schedulerEnabled = v;
else parseFailEnvName ??= EnvSchedulerEnabled;
}
var envS8SchedulerRaw = Environment.GetEnvironmentVariable(EnvS8SchedulerEnabled);
if (!string.IsNullOrWhiteSpace(envS8SchedulerRaw))
{
if (bool.TryParse(envS8SchedulerRaw, out var v)) s8SchedulerEnabled = v;
else parseFailEnvName ??= EnvS8SchedulerEnabled;
}
if (parseFailEnvName != null && Interlocked.Exchange(ref _firstParseFailLogged, 1) == 0)
{
_logger.LogWarning(
"S8TimeoutAutoEscalationJob env override 解析失败:{EnvName},已沿用配置值",
parseFailEnvName);
}
if (!schedulerEnabled || !s8SchedulerEnabled)
{
if (Interlocked.Exchange(ref _firstDisabledLogged, 1) == 0)
{
_logger.LogInformation(
"S8TimeoutAutoEscalationJob 被配置禁用:Scheduler:Enabled={Scheduler} S8:Scheduler:Enabled={S8Scheduler}",
schedulerEnabled, s8SchedulerEnabled);
}
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 执行失败");
}
}
}