aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Program.cs
diff options
context:
space:
mode:
authorCody Robibero <cody@robibe.ro>2026-09-06 07:33:07 -0400
committerGitHub <noreply@github.com>2026-09-06 07:33:07 -0400
commita0a42c630ef724fa92b61b261eab8132cb3116d7 (patch)
tree2de91390db268215f322c7e18262026064d50684 /Jellyfin.Server/Program.cs
parent66d038c4034b9e07a4ac39822e4f0080e9a2201a (diff)
parent74ce774effac4d70728e231e6de7c1c5d82a2685 (diff)
Merge pull request #17786 from Shadowghost/optimize-db
Optimize database after migrations
Diffstat (limited to 'Jellyfin.Server/Program.cs')
-rw-r--r--Jellyfin.Server/Program.cs42
1 files changed, 34 insertions, 8 deletions
diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs
index 12f92efb35..58861fc476 100644
--- a/Jellyfin.Server/Program.cs
+++ b/Jellyfin.Server/Program.cs
@@ -61,6 +61,7 @@ namespace Jellyfin.Server
private static ILogger _logger = NullLogger.Instance;
private static bool _restartOnShutdown;
private static IStartupLogger<JellyfinMigrationService>? _migrationLogger;
+ private static bool _optimizeDatabaseAfterMigration;
private static string? _restoreFromBackup;
/// <summary>
@@ -209,14 +210,15 @@ namespace Jellyfin.Server
await jellyfinMigrationService.PrepareSystemForMigration(_logger).ConfigureAwait(false);
// "Preparing migrations" carries through the DB read; per-migration progress is reported
// as "Running migration X of Y" from inside the step once the pending set is known.
- await jellyfinMigrationService.MigrateStepAsync(JellyfinMigrationStageTypes.CoreInitialisation, appHost.ServiceProvider).ConfigureAwait(false);
+ _optimizeDatabaseAfterMigration |= await jellyfinMigrationService.MigrateStepAsync(JellyfinMigrationStageTypes.CoreInitialisation, appHost.ServiceProvider).ConfigureAwait(false);
SetupServer.ReportActivity(StartupActivity.InitializingServices);
await appHost.InitializeServices(startupConfig).ConfigureAwait(false);
_appHost = appHost;
- await jellyfinMigrationService.MigrateStepAsync(JellyfinMigrationStageTypes.AppInitialisation, appHost.ServiceProvider).ConfigureAwait(false);
+ _optimizeDatabaseAfterMigration |= await jellyfinMigrationService.MigrateStepAsync(JellyfinMigrationStageTypes.AppInitialisation, appHost.ServiceProvider).ConfigureAwait(false);
await jellyfinMigrationService.CleanupSystemAfterMigration(_logger).ConfigureAwait(false);
+ await OptimizeDatabaseAfterMigrationAsync(appHost.ServiceProvider).ConfigureAwait(false);
try
{
configurationCompleted = true;
@@ -271,12 +273,11 @@ namespace Jellyfin.Server
// Don't throw additional exception if startup failed.
if (appHost.ServiceProvider is not null)
{
- _logger.LogInformation("Running query planner optimizations in the database... This might take a while");
+ _logger.LogInformation("Optimizing the database... This might take a while");
+ // Deliberately untimed: a truncated optimization leaves the statistics incomplete.
var databaseProvider = appHost.ServiceProvider.GetRequiredService<IJellyfinDatabaseProvider>();
- using var shutdownSource = new CancellationTokenSource();
- shutdownSource.CancelAfter((int)TimeSpan.FromSeconds(60).TotalMicroseconds);
- await databaseProvider.RunShutdownTask(shutdownSource.Token).ConfigureAwait(false);
+ await databaseProvider.RunShutdownTask(CancellationToken.None).ConfigureAwait(false);
}
_appHost = null;
@@ -314,7 +315,7 @@ namespace Jellyfin.Server
var jellyfinMigrationService = ActivatorUtilities.CreateInstance<JellyfinMigrationService>(startupService);
await jellyfinMigrationService.CheckFirstTimeRunOrMigration(appPaths, startupOptions).ConfigureAwait(false);
- await jellyfinMigrationService.MigrateStepAsync(Migrations.Stages.JellyfinMigrationStageTypes.PreInitialisation, startupService).ConfigureAwait(false);
+ _optimizeDatabaseAfterMigration |= await jellyfinMigrationService.MigrateStepAsync(Migrations.Stages.JellyfinMigrationStageTypes.PreInitialisation, startupService).ConfigureAwait(false);
}
/// <summary>
@@ -329,7 +330,32 @@ namespace Jellyfin.Server
public static async Task ApplyCoreMigrationsAsync(IServiceProvider serviceProvider, Migrations.Stages.JellyfinMigrationStageTypes jellyfinMigrationStage)
{
var jellyfinMigrationService = ActivatorUtilities.CreateInstance<JellyfinMigrationService>(serviceProvider, _migrationLogger!);
- await jellyfinMigrationService.MigrateStepAsync(jellyfinMigrationStage, serviceProvider).ConfigureAwait(false);
+ _optimizeDatabaseAfterMigration |= await jellyfinMigrationService.MigrateStepAsync(jellyfinMigrationStage, serviceProvider).ConfigureAwait(false);
+ }
+
+ private static async Task OptimizeDatabaseAfterMigrationAsync(IServiceProvider serviceProvider)
+ {
+ if (!_optimizeDatabaseAfterMigration)
+ {
+ return;
+ }
+
+ // Reset first: a restart runs no migrations and must not optimize again.
+ _optimizeDatabaseAfterMigration = false;
+ SetupServer.ReportActivity(StartupActivity.OptimizingDatabase);
+ _logger.LogInformation("Migrations have been applied, optimizing the database... This might take a while");
+
+ try
+ {
+ // Deliberately untimed: incomplete statistics are worse than a slow start.
+ var databaseProvider = serviceProvider.GetRequiredService<IJellyfinDatabaseProvider>();
+ await databaseProvider.RunScheduledOptimisation(CancellationToken.None).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ // A missed optimization only costs performance, so never fail startup over this.
+ _logger.LogError(ex, "Error while optimizing the database after migration");
+ }
}
/// <summary>