feat(analysis): scheduler de jobs d'analyse avec routage par capacités, priorités et dead-letter (#159)
This commit is contained in:
1 parent
760aa7baf8
commit
ca43c6d28e
15 files changed
+1696
-1
No files matched your search
@@ -0,0 +1,102 @@
|
||||
using Heimdall.Application.Services;
|
||||
using Heimdall.Domain.Enums;
|
||||
using Heimdall.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Heimdall.Infrastructure.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Hangfire job that dispatches pending analysis jobs to agents and cleans up
|
||||
/// timed-out jobs. Runs on the "analysis" queue.
|
||||
///
|
||||
/// Responsibilities:
|
||||
/// 1. Pick pending jobs ordered by priority (security first) and dispatch to best agent
|
||||
/// 2. Detect jobs stuck in Dispatched/Running past their timeout → retry or dead-letter
|
||||
///
|
||||
/// Uses IDbContextFactory to avoid sharing DbContext across Hangfire worker threads.
|
||||
/// </summary>
|
||||
public sealed class AnalysisJobDispatcherJob
|
||||
{
|
||||
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
||||
private readonly IAnalysisJobSchedulerService _schedulerService;
|
||||
private readonly ILogger<AnalysisJobDispatcherJob> _logger;
|
||||
|
||||
/// <summary>Max jobs to dispatch per tick to prevent overwhelming agents.</summary>
|
||||
private const int MaxDispatchPerTick = 10;
|
||||
|
||||
public AnalysisJobDispatcherJob(
|
||||
IDbContextFactory<AppDbContext> dbFactory,
|
||||
IAnalysisJobSchedulerService schedulerService,
|
||||
ILogger<AnalysisJobDispatcherJob> logger)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_schedulerService = schedulerService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Main dispatcher tick: process pending jobs and clean up timed-out jobs.
|
||||
/// Called every 30 seconds by Hangfire recurring job.
|
||||
/// </summary>
|
||||
public async Task ExecuteAsync(CancellationToken ct = default)
|
||||
{
|
||||
// 1. Dispatch pending jobs
|
||||
var dispatched = await _schedulerService
|
||||
.ProcessPendingJobsAsync(MaxDispatchPerTick, ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
// 2. Clean up timed-out jobs
|
||||
var timedOutJobs = await _schedulerService
|
||||
.GetTimedOutJobsAsync(ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
foreach (var job in timedOutJobs)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Analysis job {JobId} timed out (status={Status}, dispatched={DispatchedAt})",
|
||||
job.Id, job.Status, job.DispatchedAt);
|
||||
|
||||
await _schedulerService.FailJobAsync(
|
||||
job.Id,
|
||||
$"Execution exceeded timeout of {job.MaxExecutionTime.TotalSeconds}s",
|
||||
ct)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (dispatched > 0 || timedOutJobs.Count > 0)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Analysis dispatcher: {Dispatched} dispatched, {TimedOut} timed out",
|
||||
dispatched, timedOutJobs.Count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Timeout sweep only — runs less frequently (every 60s) to catch stuck jobs.
|
||||
/// </summary>
|
||||
public async Task ExecuteTimeoutSweepAsync(CancellationToken ct = default)
|
||||
{
|
||||
var timedOutJobs = await _schedulerService
|
||||
.GetTimedOutJobsAsync(ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (timedOutJobs.Count == 0)
|
||||
return;
|
||||
|
||||
foreach (var job in timedOutJobs)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Sweep: analysis job {JobId} stuck in {Status} since {DispatchedAt} (timeout={Timeout}s)",
|
||||
job.Id, job.Status, job.DispatchedAt, job.MaxExecutionTime.TotalSeconds);
|
||||
|
||||
await _schedulerService.FailJobAsync(
|
||||
job.Id,
|
||||
$"Sweep: execution exceeded timeout of {job.MaxExecutionTime.TotalSeconds}s",
|
||||
ct)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Timeout sweep: {Count} jobs cleaned up", timedOutJobs.Count);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user