Agent settings UI: - Add settings section to AgentDetail page showing per-agent overrides - Add/edit/delete agent settings with confirmation dialogs - Add settings API methods + DTOs to HeimdallApiClient - Real-time SignalR notification shown in UI feedback Migration orchestrator: - Replace inline db.Database.Migrate() with MigrationHostedService - Runs migrations in background at startup (non-blocking) - 60-second timeout with critical logging on failure - Preserves Testing environment exclusion - Clear migration progress logging
75 lines
2.7 KiB
C#
75 lines
2.7 KiB
C#
using Heimdall.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Heimdall.Infrastructure.Services;
|
|
|
|
/// <summary>
|
|
/// Applies pending EF Core migrations as a background service at startup.
|
|
/// Replaces the inline `db.Database.Migrate()` pattern to:
|
|
/// - Allow the web host to start quickly (migrations run in background)
|
|
/// - Enable health checks to verify migration completion
|
|
/// - Avoid holding up the startup pipeline
|
|
/// - Log migration progress clearly
|
|
/// </summary>
|
|
public class MigrationHostedService : IHostedService
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly ILogger<MigrationHostedService> _logger;
|
|
|
|
public MigrationHostedService(
|
|
IServiceProvider serviceProvider,
|
|
ILogger<MigrationHostedService> logger)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("Migration service starting...");
|
|
|
|
try
|
|
{
|
|
using var scope = _serviceProvider.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
|
|
var pending = await db.Database.GetPendingMigrationsAsync(cancellationToken);
|
|
var pendingList = pending.ToList();
|
|
|
|
if (pendingList.Count == 0)
|
|
{
|
|
_logger.LogInformation("No pending migrations to apply");
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation(
|
|
"Applying {Count} pending migration(s): {Migrations}",
|
|
pendingList.Count,
|
|
string.Join(", ", pendingList));
|
|
|
|
// Apply with timeout to prevent indefinite blocking
|
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
cts.CancelAfter(TimeSpan.FromSeconds(60));
|
|
|
|
await db.Database.MigrateAsync(cts.Token);
|
|
|
|
_logger.LogInformation("All migrations applied successfully");
|
|
}
|
|
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
_logger.LogError("Migration timed out after 60 seconds. " +
|
|
"Check database connectivity and migration script correctness.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogCritical(ex, "Failed to apply database migrations. " +
|
|
"The application may not function correctly without a migrated database.");
|
|
}
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
}
|