Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a cronjob functionality intended for InspectionReportGeneration #1218

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class FlotillaDbContext(DbContextOptions options) : DbContext(options)
public DbSet<MissionRun> MissionRuns => Set<MissionRun>();
public DbSet<MissionTask> MissionTasks => Set<MissionTask>();
public DbSet<Inspection> Inspections => Set<Inspection>();
public DbSet<InspectionFinding> InspectionFindings => Set<InspectionFinding>();
public DbSet<MissionDefinition> MissionDefinitions => Set<MissionDefinition>();
public DbSet<Plant> Plants => Set<Plant>();
public DbSet<Installation> Installations => Set<Installation>();
Expand Down Expand Up @@ -48,11 +49,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
);
});
modelBuilder.Entity<Inspection>(inspectionEntity =>
{
if (isSqlLite) { AddConverterForDateTimeOffsets(ref inspectionEntity); }
inspectionEntity.OwnsMany(i => i.InspectionFindings);
});

modelBuilder.Entity<MissionDefinition>()
.Property(m => m.InspectionFrequency)
Expand Down
34 changes: 34 additions & 0 deletions backend/api/EventHandlers/InspectionFindingsEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Api.Services;
namespace Api.EventHandlers
{
public class InspectionFindingEventHandler(IConfiguration configuration,
IServiceScopeFactory scopeFactory,
ILogger<InspectionFindingEventHandler> logger) : BackgroundService
{
private readonly TimeSpan _interval = configuration.GetValue<TimeSpan>("InspectionFindingEventHandler:Interval");
private InspectionFindingService InspectionFindingService => scopeFactory.CreateScope().ServiceProvider.GetRequiredService<InspectionFindingService>();
private readonly TimeSpan _timeSpan = configuration.GetValue<TimeSpan>("InspectionFindingEventHandler:TimeSpan");

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
await Task.Delay(_interval, stoppingToken);

var lastReportingTime = DateTime.UtcNow - _timeSpan;

var inspectionFindings = await InspectionFindingService.RetrieveInspectionFindings(lastReportingTime);

logger.LogInformation("Found {count} inspection findings in the last {interval}.", inspectionFindings.Count, _timeSpan);

}
catch (OperationCanceledException)
{
throw;
}
}
}
}
}
Loading
Loading