Merge remote-tracking branch 'origin/fix/config-broadcast-di' into audit/snapshot-gallery
This commit is contained in:
commit
de7efca59e
1 file changed
+21
-8
@@ -12,14 +12,14 @@ namespace Heimdall.Infrastructure.Data;
|
||||
public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>, IApplicationDbContext
|
||||
{
|
||||
private readonly AuditInterceptor _auditInterceptor;
|
||||
private readonly IConfigBroadcastService? _configBroadcast;
|
||||
private readonly IServiceProvider? _serviceProvider;
|
||||
|
||||
public AppDbContext(
|
||||
DbContextOptions<AppDbContext> options,
|
||||
AuditInterceptor auditInterceptor, IConfigBroadcastService? configBroadcast = null) : base(options)
|
||||
AuditInterceptor auditInterceptor, IServiceProvider? serviceProvider = null) : base(options)
|
||||
{
|
||||
_auditInterceptor = auditInterceptor;
|
||||
_configBroadcast = configBroadcast;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
// Domain entities
|
||||
@@ -110,12 +110,25 @@ public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>, IApplicat
|
||||
e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted &&
|
||||
e.Entity is Camera or CameraAssignment or CameraDetectionProfile);
|
||||
var result = await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (invalidate && _configBroadcast is not null)
|
||||
if (invalidate)
|
||||
{
|
||||
// Payload-free invalidation: each agent refetches its authorized full snapshot.
|
||||
// Delivery is best effort; reconnect and periodic resync recover a missed notification.
|
||||
try { await _configBroadcast.NotifyAllAgentsReload(cancellationToken).ConfigureAwait(false); }
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException) { System.Diagnostics.Trace.TraceError("Config invalidation delivery failed: {0}", ex.GetType().Name); }
|
||||
// Resolve only after this DbContext is fully constructed. The broadcast service
|
||||
// depends on IApplicationDbContext, which maps back to this scoped instance.
|
||||
// Resolving it in the constructor creates a circular dependency during startup.
|
||||
try
|
||||
{
|
||||
var configBroadcast = _serviceProvider?.GetService(typeof(IConfigBroadcastService)) as IConfigBroadcastService;
|
||||
if (configBroadcast is not null)
|
||||
{
|
||||
// Payload-free invalidation: each agent refetches its authorized full snapshot.
|
||||
// Delivery is best effort; reconnect and periodic resync recover a missed notification.
|
||||
await configBroadcast.NotifyAllAgentsReload(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
System.Diagnostics.Trace.TraceError("Config invalidation delivery failed: {0}", ex.GetType().Name);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user