diff --git a/src/Heimdall.Infrastructure/Services/AnalysisJobSchedulerService.cs b/src/Heimdall.Infrastructure/Services/AnalysisJobSchedulerService.cs index f21cf83..a509a9d 100644 --- a/src/Heimdall.Infrastructure/Services/AnalysisJobSchedulerService.cs +++ b/src/Heimdall.Infrastructure/Services/AnalysisJobSchedulerService.cs @@ -280,7 +280,7 @@ public sealed class AnalysisJobSchedulerService : IAnalysisJobSchedulerService Agent = a, Score = ComputeAgentScore(a, job, workloadMap) }) - .Where(x => x.Score >= 0) // -1 means disqualified + .Where(x => x.Score.HasValue) .OrderBy(x => x.Score) .ThenBy(x => workloadMap.TryGetValue(x.Agent.Id, out var c) ? c : 0) .ToList(); @@ -300,19 +300,19 @@ public sealed class AnalysisJobSchedulerService : IAnalysisJobSchedulerService /// - RAM: 0 if sufficient, +10 per missing GB /// - Load: +count of active jobs (prefer less loaded agents) /// - private static int ComputeAgentScore(Agent agent, AnalysisJob job, Dictionary workload) + private static int? ComputeAgentScore(Agent agent, AnalysisJob job, Dictionary workload) { // Disqualify: GPU required but agent doesn't have it if (job.RequiresGpu && !agent.HasGpu) - return -1; + return null; // Disqualify: CPU cores insufficient if (job.RequiredCpuCores > 0 && agent.CpuCores < job.RequiredCpuCores) - return -1; + return null; // Disqualify: RAM insufficient if (job.RequiredMemoryMb > 0 && agent.MemoryMb < job.RequiredMemoryMb) - return -1; + return null; var score = 0; diff --git a/tests/Heimdall.Infrastructure.Tests/AnalysisJobSchedulerServiceTests.cs b/tests/Heimdall.Infrastructure.Tests/AnalysisJobSchedulerServiceTests.cs index 52f5ebf..8540e04 100644 --- a/tests/Heimdall.Infrastructure.Tests/AnalysisJobSchedulerServiceTests.cs +++ b/tests/Heimdall.Infrastructure.Tests/AnalysisJobSchedulerServiceTests.cs @@ -53,6 +53,35 @@ public class AnalysisJobSchedulerServiceTests : IAsyncLifetime return new TestDbContextFactory(opts); } + public static IEnumerable GpuScenarios => + from type in Enum.GetValues() + from sameSite in new[] { true, false } + from loaded in new[] { true, false } + select new object[] { type, sameSite, loaded }; + + [Theory] + [MemberData(nameof(GpuScenarios))] + public async Task SelectBestAgent_SoleGpu_RemainsEligible(AnalysisJobType type, bool sameSite, bool loaded) + { + var (siteId, cameraId) = await SeedSiteAndCameraAsync(); + await using var db = await _dbFactory.CreateDbContextAsync(); + var otherSite = new Site("remote", "remote", "UTC"); + if (!sameSite) db.Sites.Add(otherSite); + var agent = new Agent(sameSite ? siteId : otherSite.Id, "gpu", "gpu", "1.0"); + agent.Register("hash", "prefix"); + agent.UpdateCapabilities(true, 8, 8192, GpuAcceleration.NvidiaCuda); + db.Agents.Add(agent); + if (loaded) + { + var active = new AnalysisJob(cameraId, siteId, type, AnalysisJobPriority.Periodic); + active.Dispatch(agent.Id); + db.AnalysisJobs.Add(active); + } + await db.SaveChangesAsync(); + var created = await _service.CreateJobAsync(cameraId, type, AnalysisJobPriority.Periodic); + (await _service.SelectBestAgentAsync(created.Data!, CancellationToken.None))!.Id.ShouldBe(agent.Id); + } + // ── CreateJob ─────────────────────────────────────────────────── [Fact]