fix: resolve GlitchTip anomalies — DB retry, query filters, Sentry DSN, Workers tracking

- 🔴 Fix startup crash when DB is temporarily unavailable: add 5x retry
  with exponential backoff to Database.MigrateAsync() in API and Workers
- 🔴 Fix EF Core 'required navigation with query filter' warnings:
  add matching soft-delete query filters to AgentSetting, CameraCredentials,
  CameraDetectionProfile, UserSite
- 🔴 Add Sentry error tracking to Workers (Hangfire background jobs)
- 🟡 Make Sentry/GlitchTip DSN configurable via SENTRY_DSN env var
  (applies to API, Web, Agent, Workers)
- 🟡 Suppress EF Core RedundantIndexRemoved informational messages

Closes GlitchTip #538
This commit is contained in:
Arnaud Dartois committed 2026-07-18 20:31:37 +00:00
1 parent ee66cf5971
commit fdfd506977
7 files changed
+108 -34

No files matched your search

@@ -3,6 +3,7 @@ using Heimdall.Infrastructure.Data.Interceptors;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace Heimdall.Infrastructure.Data;
@@ -28,10 +29,19 @@ public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>
builder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
// Global soft-delete filter
// Global soft-delete filter — principal entities
builder.Entity<Site>().HasQueryFilter(e => !e.IsDeleted);
builder.Entity<Agent>().HasQueryFilter(e => !e.IsDeleted);
builder.Entity<Camera>().HasQueryFilter(e => !e.IsDeleted);
// Matching query filters on dependent entities to prevent
// "required navigation with query filter" EF Core warnings.
// Without these, loading an AgentSetting whose Agent was soft-deleted
// would cause a null reference on the required navigation.
builder.Entity<AgentSetting>().HasQueryFilter(e => e.Agent == null || !e.Agent.IsDeleted);
builder.Entity<CameraCredentials>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
builder.Entity<CameraDetectionProfile>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
builder.Entity<UserSite>().HasQueryFilter(e => e.Site == null || !e.Site.IsDeleted);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
@@ -40,6 +50,15 @@ public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>
new AuditInterceptor(),
new SoftDeleteInterceptor());
// Suppress informational EF Core messages that create noise in
// error tracking (GlitchTip #538). Redundant index warnings are
// automatically handled by EF Core — it simply skips creating FK
// indexes that are already covered by composite indexes/PKs.
optionsBuilder.ConfigureWarnings(warnings =>
{
warnings.Ignore(CoreEventId.RedundantIndexRemoved);
});
base.OnConfiguring(optionsBuilder);
}
}