Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update container startup timeouts due to potential long-running EF migrations (#2159)
Leendert de Borstb4f2eb7f78b7
diff --git a/apps/server/AliasVault.Api/Program.cs b/apps/server/AliasVault.Api/Program.csindex 1db3d14ae..06800c841 100644--- a/apps/server/AliasVault.Api/Program.cs+++ b/apps/server/AliasVault.Api/Program.cs@@ -225,8 +225,8 @@ using (var scope = app.Services.CreateScope())var migrationLogger = container.GetRequiredService<ILoggerFactory>().CreateLogger("AliasVault.DatabaseMigrations");await using var db = await container.GetRequiredService<IAliasServerDbContextFactory>().CreateDbContextAsync();- // Raise the command timeout for migrations.- db.Database.SetCommandTimeout((int)TimeSpan.FromMinutes(60).TotalSeconds);+ // The API is responsible for database migrations and runtime should not be limited by a command timeout.+ db.Database.SetCommandTimeout(0);await db.MigrateWithLoggingAsync(migrationLogger);}diff --git a/apps/server/Databases/AliasServerDb/Configuration/DatabaseConfiguration.cs b/apps/server/Databases/AliasServerDb/Configuration/DatabaseConfiguration.csindex 0780015c9..8a7979996 100644--- a/apps/server/Databases/AliasServerDb/Configuration/DatabaseConfiguration.cs+++ b/apps/server/Databases/AliasServerDb/Configuration/DatabaseConfiguration.cs@@ -106,86 +106,117 @@ public static class DatabaseConfiguration}/// <summary>- /// Waits for the database to be ready by checking if all migrations have been applied.- /// This is useful for services that should not run migrations themselves but need to wait- /// for another service (typically the API) to complete migrations first.+ /// Waits until all migrations are applied by the API service./// </summary>/// <param name="context">The database context to check.</param>/// <param name="logger">Optional logger for diagnostics.</param>- /// <param name="timeoutSeconds">Maximum time to wait in seconds (default: 60).</param>+ /// <param name="timeoutSeconds">Maximum time to wait without seeing a pending migration (default: 60).</param>/// <param name="checkIntervalMs">Interval between checks in milliseconds (default: 2000).</param>/// <returns>A task representing the asynchronous operation.</returns>public static async Task WaitForDatabaseReadyAsync(this DbContext context, ILogger? logger = null, int timeoutSeconds = 60, int checkIntervalMs = 2000){var timeout = DateTime.UtcNow.AddSeconds(timeoutSeconds);+ var waitStopwatch = Stopwatch.StartNew();+ var lastProgressLog = TimeSpan.FromSeconds(-30);var attempt = 0;- while (DateTime.UtcNow < timeout)+ while (true){attempt++;try{// First check if database is accessible- var canConnect = await context.Database.CanConnectAsync();- if (!canConnect)+ if (!await context.Database.CanConnectAsync()){- logger?.LogInformation(- "Database not yet accessible. Attempt {Attempt}. Waiting {Interval}ms...",- attempt,- checkIntervalMs);- await Task.Delay(checkIntervalMs);- continue;+ logger?.LogInformation("Database not yet accessible. Attempt {Attempt}. Waiting {Interval}ms...", attempt, checkIntervalMs);}-- // Check if migrations history table exists to avoid PostgreSQL logging errors- var connection = context.Database.GetDbConnection();- await using var command = connection.CreateCommand();- command.CommandText = "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '__EFMigrationsHistory')";-- if (connection.State != System.Data.ConnectionState.Open)- {- await connection.OpenAsync();- }-- var tableExists = (bool)(await command.ExecuteScalarAsync() ?? false);-- if (!tableExists)+ else{- logger?.LogInformation(- "Database accessible but migrations not yet started. Attempt {Attempt}. Waiting {Interval}ms...",- attempt,- checkIntervalMs);- await Task.Delay(checkIntervalMs);- continue;+ var pendingCount = await GetPendingMigrationCountAsync(context);+ if (pendingCount == 0)+ {+ logger?.LogInformation("Database is ready. All migrations have been applied.");+ return;+ }++ if (pendingCount is null)+ {+ logger?.LogInformation("Database accessible but migrations not yet started. Attempt {Attempt}. Waiting {Interval}ms...", attempt, checkIntervalMs);+ }+ else+ {+ timeout = DateTime.UtcNow.AddSeconds(timeoutSeconds);+ if (waitStopwatch.Elapsed - lastProgressLog >= TimeSpan.FromSeconds(30))+ {+ lastProgressLog = waitStopwatch.Elapsed;+ logger?.LogInformation("Waiting for the API to apply {PendingCount} pending database migration(s), current wait time: {Duration}.", pendingCount, FormatDuration(waitStopwatch.Elapsed));+ }+ }}-- // Now safe to check pending migrations without PostgreSQL logging errors- var pendingMigrations = await context.Database.GetPendingMigrationsAsync();- if (!pendingMigrations.Any())+ }+ catch (PostgresException ex) when (ex.SqlState == PostgresErrorCodes.LockNotAvailable)+ {+ // The migration history table is locked, which means the API is applying a migration right now.+ timeout = DateTime.UtcNow.AddSeconds(timeoutSeconds);+ if (waitStopwatch.Elapsed - lastProgressLog >= TimeSpan.FromSeconds(30)){- logger?.LogInformation("Database is ready. All migrations have been applied.");- return;+ lastProgressLog = waitStopwatch.Elapsed;+ logger?.LogInformation("Waiting for the API to finish applying database migrations, current wait time: {Duration}.", FormatDuration(waitStopwatch.Elapsed));}-- logger?.LogInformation(- "Waiting for database migrations to complete. {PendingCount} migrations pending. Attempt {Attempt}.",- pendingMigrations.Count(),- attempt);}catch (Exception ex){- logger?.LogWarning(- ex,- "Error checking database status. Attempt {Attempt}. Waiting {Interval}ms before retry...",- attempt,- checkIntervalMs);+ logger?.LogWarning(ex, "Error checking database status. Attempt {Attempt}. Waiting {Interval}ms before retry...", attempt, checkIntervalMs);+ }++ if (DateTime.UtcNow >= timeout)+ {+ throw new TimeoutException($"Database did not become ready within {timeoutSeconds} seconds and no pending migrations were detected. Is the API running?");}await Task.Delay(checkIntervalMs);}+ }++ /// <summary>+ /// Gets the number of migrations not yet applied, or null when the migration history table does not exist yet.+ /// </summary>+ /// <param name="context">The database context to check.</param>+ /// <returns>The pending migration count, or null when migrations have not started yet.</returns>+ private static async Task<int?> GetPendingMigrationCountAsync(DbContext context)+ {+ var connection = context.Database.GetDbConnection();+ if (connection.State != System.Data.ConnectionState.Open)+ {+ await connection.OpenAsync();+ }++ await using var transaction = await connection.BeginTransactionAsync();+ await using var command = connection.CreateCommand();+ command.Transaction = transaction;++ // Check if migrations history table exists to avoid PostgreSQL logging errors+ command.CommandText = "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '__EFMigrationsHistory')";+ if (!(bool)(await command.ExecuteScalarAsync() ?? false))+ {+ return null;+ }++ command.CommandText = "SET LOCAL lock_timeout = '2s'";+ await command.ExecuteNonQueryAsync();++ command.CommandText = "SELECT \"MigrationId\" FROM \"__EFMigrationsHistory\"";+ var applied = new HashSet<string>();+ await using (var reader = await command.ExecuteReaderAsync())+ {+ while (await reader.ReadAsync())+ {+ applied.Add(reader.GetString(0));+ }+ }- throw new TimeoutException($"Database did not become ready within {timeoutSeconds} seconds. Migrations may not have completed.");+ return context.Database.GetMigrations().Count(m => !applied.Contains(m));}/// <summary>