feat(analysis): scheduler de jobs d'analyse avec routage par capacités, priorités et dead-letter (#159)

This commit is contained in:
Arnaud Dartois authored and Arnaud Dartois committed 2026-08-26 23:44:38 +02:00
1 parent 760aa7baf8
commit ca43c6d28e
15 files changed
+1696 -1

No files matched your search

@@ -66,6 +66,9 @@ public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>, IApplicat
public DbSet<LicensePlate> LicensePlates => Set<LicensePlate>();
public DbSet<StoragePolicy> StoragePolicies => Set<StoragePolicy>();
// Analysis job scheduling
public DbSet<AnalysisJob> AnalysisJobs => Set<AnalysisJob>();
public async Task SaveChangesWithRetryAsync(int maxRetries = 3, CancellationToken ct = default)
{
await DbContextExtensions.SaveChangesWithRetryAsync(this, maxRetries, ct).ConfigureAwait(false);
@@ -98,6 +101,7 @@ public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>, IApplicat
builder.Entity<CameraDiscoveryResult>().HasQueryFilter(e => e.Site == null || !e.Site.IsDeleted);
builder.Entity<CameraEvent>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
builder.Entity<DiscoveredStreamUrl>().HasQueryFilter(e => e.DiscoveryResult == null || e.DiscoveryResult.Site == null || !e.DiscoveryResult.Site.IsDeleted);
builder.Entity<AnalysisJob>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
// Group soft-delete filters
builder.Entity<Group>().HasQueryFilter(e => !e.IsDeleted);
@@ -0,0 +1,90 @@
using Heimdall.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Heimdall.Infrastructure.Data.Configurations;
public class AnalysisJobConfiguration : IEntityTypeConfiguration<AnalysisJob>
{
public void Configure(EntityTypeBuilder<AnalysisJob> builder)
{
builder.ToTable("analysis_jobs");
builder.HasKey(j => j.Id);
// Core fields
builder.Property(j => j.JobType)
.HasConversion<string>()
.HasMaxLength(50)
.IsRequired();
builder.Property(j => j.Status)
.HasConversion<string>()
.HasMaxLength(30)
.IsRequired();
builder.Property(j => j.Priority)
.HasConversion<int>()
.IsRequired();
// Capability requirements
builder.Property(j => j.RequiredCpuCores)
.HasDefaultValue(0);
builder.Property(j => j.RequiredMemoryMb)
.HasDefaultValue(0L);
// Timeout stored as ticks (TimeSpan → long)
builder.Property(j => j.MaxExecutionTime)
.HasConversion(
v => v.Ticks,
v => new TimeSpan(v))
.HasDefaultValue(TimeSpan.FromMinutes(5));
// Retry
builder.Property(j => j.RetryCount)
.HasDefaultValue(0);
builder.Property(j => j.MaxRetries)
.HasDefaultValue(3);
// JSON payloads
builder.Property(j => j.ParametersJson)
.HasColumnType("jsonb");
builder.Property(j => j.ResultJson)
.HasColumnType("jsonb");
builder.Property(j => j.ErrorMessage)
.HasMaxLength(2000);
// Indexes
// Primary lookup: pending jobs ordered by priority + creation time
builder.HasIndex(j => new { j.Status, j.Priority, j.CreatedAt })
.HasDatabaseName("ix_analysis_jobs_pending");
// Timeout sweep: dispatched/running jobs with DispatchedAt
builder.HasIndex(j => new { j.Status, j.DispatchedAt })
.HasDatabaseName("ix_analysis_jobs_timeout");
// Agent workload queries
builder.HasIndex(j => new { j.AssignedAgentId, j.Status })
.HasDatabaseName("ix_analysis_jobs_agent_workload")
.HasFilter("\"AssignedAgentId\" IS NOT NULL");
// Camera lookup
builder.HasIndex(j => j.CameraId)
.HasDatabaseName("ix_analysis_jobs_camera");
// Relationships
builder.HasOne(j => j.Camera)
.WithMany()
.HasForeignKey(j => j.CameraId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(j => j.AssignedAgent)
.WithMany()
.HasForeignKey(j => j.AssignedAgentId)
.OnDelete(DeleteBehavior.SetNull);
}
}