diff --git a/backend/api/EventHandlers/InspectionFindingsEventHandler.cs b/backend/api/EventHandlers/InspectionFindingsEventHandler.cs index e18392cb8..6f13a6e17 100644 --- a/backend/api/EventHandlers/InspectionFindingsEventHandler.cs +++ b/backend/api/EventHandlers/InspectionFindingsEventHandler.cs @@ -1,5 +1,4 @@ -using Api.Database.Context; -using Api.Services; +using Api.Services; namespace Api.EventHandlers { public class InspectionFindingEventHandler(IConfiguration configuration, @@ -7,22 +6,20 @@ public class InspectionFindingEventHandler(IConfiguration configuration, ILogger logger) : BackgroundService { private readonly TimeSpan _interval = configuration.GetValue("InspectionFindingEventHandler:Interval"); - private IInspectionFindingService InspectionFindingService => scopeFactory.CreateScope().ServiceProvider.GetRequiredService(); + private InspectionFindingService InspectionFindingService => scopeFactory.CreateScope().ServiceProvider.GetRequiredService(); private readonly TimeSpan _timeSpan = configuration.GetValue("InspectionFindingEventHandler:TimeSpan"); protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - - while (!stoppingToken.IsCancellationRequested) { try { await Task.Delay(_interval, stoppingToken); - using var scope = scopeFactory.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - var inspectionFindings = await InspectionFindingService.RetrieveInspectionFindings(_timeSpan, context); + var lastReportingTime = DateTime.UtcNow - _timeSpan; + + var inspectionFindings = await InspectionFindingService.RetrieveInspectionFindings(lastReportingTime); logger.LogInformation("Found {count} inspection findings in the last {interval}.", inspectionFindings.Count, _timeSpan); @@ -33,6 +30,5 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } } } - } } diff --git a/backend/api/Program.cs b/backend/api/Program.cs index 28d006081..820380b60 100644 --- a/backend/api/Program.cs +++ b/backend/api/Program.cs @@ -73,7 +73,6 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); - bool useInMemoryDatabase = builder.Configuration .GetSection("Database") .GetValue("UseInMemoryDatabase"); @@ -89,11 +88,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddTransient(); - builder.Services.AddHostedService(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); diff --git a/backend/api/Services/InspectionFindingsService.cs b/backend/api/Services/InspectionFindingsService.cs index a89959cfc..de3f48b4f 100644 --- a/backend/api/Services/InspectionFindingsService.cs +++ b/backend/api/Services/InspectionFindingsService.cs @@ -2,20 +2,16 @@ using Api.Database.Models; using Microsoft.EntityFrameworkCore; - namespace Api.Services { - public class IInspectionFindingService() + public class InspectionFindingService(FlotillaDbContext context) { - - public async Task> RetrieveInspectionFindings(TimeSpan timeSpan, FlotillaDbContext context) + public async Task> RetrieveInspectionFindings(DateTime lastReportingTime) { - var lastReportingTime = DateTime.UtcNow - timeSpan; var inspectionFindings = await context.InspectionFindings .Where(f => f.InspectionDate > lastReportingTime) .ToListAsync(); return inspectionFindings; } - } }