diff --git a/Jellyfin.Server/CoreAppHost.cs b/Jellyfin.Server/CoreAppHost.cs index 7f4bd3dea..8b4b61e29 100644 --- a/Jellyfin.Server/CoreAppHost.cs +++ b/Jellyfin.Server/CoreAppHost.cs @@ -57,33 +57,5 @@ namespace Jellyfin.Server /// protected override void ShutdownInternal() => Program.Shutdown(); - - /// - /// Runs the migration routines if necessary. - /// - public void TryMigrate() - { - var previousVersion = ConfigurationManager.CommonConfiguration.PreviousVersion; - switch (ApplicationVersion.CompareTo(previousVersion)) - { - case 1: - Logger.LogWarning("Version check shows Jellyfin was updated: previous version={0}, current version={1}", previousVersion, ApplicationVersion); - - Migrations.MigrationRunner.Run(this, Logger); - - ConfigurationManager.CommonConfiguration.PreviousVersion = ApplicationVersion; - ConfigurationManager.SaveConfiguration(); - break; - case 0: - // nothing to do, versions match - break; - case -1: - Logger.LogWarning("Version check shows Jellyfin was rolled back, use at your own risk: previous version={0}, current version={1}", previousVersion, ApplicationVersion); - // no "rollback" routines for now - ConfigurationManager.CommonConfiguration.PreviousVersion = ApplicationVersion; - ConfigurationManager.SaveConfiguration(); - break; - } - } } } diff --git a/Jellyfin.Server/Migrations/Pre_10_5.cs b/Jellyfin.Server/Migrations/DisableTranscodingThrottling.cs similarity index 79% rename from Jellyfin.Server/Migrations/Pre_10_5.cs rename to Jellyfin.Server/Migrations/DisableTranscodingThrottling.cs index 5389a2ad9..83624bdad 100644 --- a/Jellyfin.Server/Migrations/Pre_10_5.cs +++ b/Jellyfin.Server/Migrations/DisableTranscodingThrottling.cs @@ -1,6 +1,8 @@ using System; +using System.IO; using MediaBrowser.Common.Configuration; using MediaBrowser.Model.Configuration; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace Jellyfin.Server.Migrations @@ -8,13 +10,13 @@ namespace Jellyfin.Server.Migrations /// /// Updater that takes care of bringing configuration up to 10.5.0 standards. /// - internal class Pre_10_5 : IUpdater + internal class DisableTranscodingThrottling : IUpdater { /// - public Version Maximum { get => Version.Parse("10.5.0"); } + public string Name => "DisableTranscodingThrottling"; /// - public bool Perform(CoreAppHost host, ILogger logger, Version from) + public void Perform(CoreAppHost host, ILogger logger) { // Set EnableThrottling to false as it wasn't used before, and in 10.5.0 it may introduce issues var encoding = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration("encoding"); @@ -24,10 +26,7 @@ namespace Jellyfin.Server.Migrations encoding.EnableThrottling = false; host.ServerConfigurationManager.SaveConfiguration("encoding", encoding); - return true; } - - return false; } } } diff --git a/Jellyfin.Server/Migrations/DisableZealousLogging.cs b/Jellyfin.Server/Migrations/DisableZealousLogging.cs new file mode 100644 index 000000000..a0a934d4a --- /dev/null +++ b/Jellyfin.Server/Migrations/DisableZealousLogging.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Model.Configuration; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; +using ILogger = Microsoft.Extensions.Logging.ILogger; + +namespace Jellyfin.Server.Migrations +{ + /// + /// Updater that takes care of bringing configuration up to 10.5.0 standards. + /// + internal class DisableZealousLogging : IUpdater + { + /// + public string Name => "DisableZealousLogging"; + + /// + // This tones down logging from some components + public void Perform(CoreAppHost host, ILogger logger) + { + string configPath = Path.Combine(host.ServerConfigurationManager.ApplicationPaths.ConfigurationDirectoryPath, Program.LoggingConfigFile); + // TODO: fix up the config + throw new NotImplementedException("don't know how to fix logging yet"); + } + } +} diff --git a/Jellyfin.Server/Migrations/IUpdater.cs b/Jellyfin.Server/Migrations/IUpdater.cs index 60d970256..10ada73d5 100644 --- a/Jellyfin.Server/Migrations/IUpdater.cs +++ b/Jellyfin.Server/Migrations/IUpdater.cs @@ -3,24 +3,21 @@ using Microsoft.Extensions.Logging; namespace Jellyfin.Server.Migrations { + /// + /// Interface that descibes a migration routine. + /// + internal interface IUpdater + { /// - /// Interface that descibes a migration routine. + /// Gets the name of the migration, must be unique. /// - internal interface IUpdater - { - /// - /// Gets maximum version this Updater applies to. - /// If current version is greater or equal to it, skip the updater. - /// - public abstract Version Maximum { get; } + public abstract string Name { get; } - /// - /// Execute the migration from version "from". - /// - /// Host that hosts current version. - /// Host logger. - /// Version to migrate from. - /// Whether configuration was changed. - public abstract bool Perform(CoreAppHost host, ILogger logger, Version from); - } + /// + /// Execute the migration from version "from". + /// + /// Host that hosts current version. + /// Host logger. + public abstract void Perform(CoreAppHost host, ILogger logger); + } } diff --git a/Jellyfin.Server/Migrations/MigrationOptions.cs b/Jellyfin.Server/Migrations/MigrationOptions.cs new file mode 100644 index 000000000..b96288cc1 --- /dev/null +++ b/Jellyfin.Server/Migrations/MigrationOptions.cs @@ -0,0 +1,23 @@ +namespace Jellyfin.Server.Migrations +{ + /// + /// Configuration part that holds all migrations that were applied. + /// + public class MigrationOptions + { + /// + /// Initializes a new instance of the class. + /// + public MigrationOptions() + { + Applied = System.Array.Empty(); + } + +#pragma warning disable CA1819 // Properties should not return arrays + /// + /// Gets or sets he list of applied migration routine names. + /// + public string[] Applied { get; set; } +#pragma warning restore CA1819 // Properties should not return arrays + } +} diff --git a/Jellyfin.Server/Migrations/MigrationRunner.cs b/Jellyfin.Server/Migrations/MigrationRunner.cs index ad54fa38e..04d037852 100644 --- a/Jellyfin.Server/Migrations/MigrationRunner.cs +++ b/Jellyfin.Server/Migrations/MigrationRunner.cs @@ -1,3 +1,6 @@ +using System; +using System.Linq; +using MediaBrowser.Common.Configuration; using Microsoft.Extensions.Logging; namespace Jellyfin.Server.Migrations @@ -5,42 +8,56 @@ namespace Jellyfin.Server.Migrations /// /// The class that knows how migrate between different Jellyfin versions. /// - public static class MigrationRunner + public sealed class MigrationRunner { - private static readonly IUpdater[] _migrations = + /// + /// The list of known migrations, in order of applicability. + /// + internal static readonly IUpdater[] Migrations = { - new Pre_10_5() + new DisableTranscodingThrottling(), + new DisableZealousLogging() }; /// /// Run all needed migrations. /// /// CoreAppHost that hosts current version. - /// AppHost logger. - /// Whether anything was changed. - public static bool Run(CoreAppHost host, ILogger logger) + /// Factory for making the logger. + public static void Run(CoreAppHost host, ILoggerFactory loggerFactory) { - bool updated = false; - var version = host.ServerConfigurationManager.CommonConfiguration.PreviousVersion; + var logger = loggerFactory.CreateLogger(); + var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration("migrations"); + var applied = migrationOptions.Applied.ToList(); - for (var i = 0; i < _migrations.Length; i++) + for (var i = 0; i < Migrations.Length; i++) { - var updater = _migrations[i]; - if (version.CompareTo(updater.Maximum) >= 0) + var updater = Migrations[i]; + if (applied.Contains(updater.Name)) { - logger.LogDebug("Skipping updater {0} as current version {1} >= its maximum applicable version {2}", updater, version, updater.Maximum); + logger.LogDebug("Skipping migration {0} as it is already applied", updater.Name); continue; } - if (updater.Perform(host, logger, version)) + try { - updated = true; + updater.Perform(host, logger); + } + catch (Exception ex) + { + logger.LogError(ex, "Cannot apply migration {0}", updater.Name); + continue; } - version = updater.Maximum; + applied.Add(updater.Name); } - return updated; + if (applied.Count > migrationOptions.Applied.Length) + { + logger.LogInformation("Some migrations were run, saving the state"); + migrationOptions.Applied = applied.ToArray(); + host.ServerConfigurationManager.SaveConfiguration("migrations", migrationOptions); + } } } } diff --git a/Jellyfin.Server/Migrations/MigrationsFactory.cs b/Jellyfin.Server/Migrations/MigrationsFactory.cs new file mode 100644 index 000000000..ed01dc646 --- /dev/null +++ b/Jellyfin.Server/Migrations/MigrationsFactory.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using MediaBrowser.Common.Configuration; + +namespace Jellyfin.Server.Migrations +{ + /// + /// A factory that teachs Jellyfin how to find a peristent file which lists all applied migrations. + /// + public class MigrationsFactory : IConfigurationFactory + { + /// + public IEnumerable GetConfigurations() + { + return new[] + { + new MigrationsListStore() + }; + } + } +} diff --git a/Jellyfin.Server/Migrations/MigrationsListStore.cs b/Jellyfin.Server/Migrations/MigrationsListStore.cs new file mode 100644 index 000000000..d91d602c1 --- /dev/null +++ b/Jellyfin.Server/Migrations/MigrationsListStore.cs @@ -0,0 +1,19 @@ +using MediaBrowser.Common.Configuration; + +namespace Jellyfin.Server.Migrations +{ + /// + /// A configuration that lists all the migration routines that were applied. + /// + public class MigrationsListStore : ConfigurationStore + { + /// + /// Initializes a new instance of the class. + /// + public MigrationsListStore() + { + ConfigurationType = typeof(MigrationOptions); + Key = "migrations"; + } + } +} diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index aa1bdb169..027186105 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -38,6 +38,11 @@ namespace Jellyfin.Server /// public static class Program { + /// + /// The name of logging configuration file. + /// + public static readonly string LoggingConfigFile = "logging.json"; + private static readonly CancellationTokenSource _tokenSource = new CancellationTokenSource(); private static readonly ILoggerFactory _loggerFactory = new SerilogLoggerFactory(); private static ILogger _logger = NullLogger.Instance; @@ -182,7 +187,7 @@ namespace Jellyfin.Server // A bit hacky to re-use service provider since ASP.NET doesn't allow a custom service collection. appHost.ServiceProvider = host.Services; appHost.FindParts(); - appHost.TryMigrate(); + Migrations.MigrationRunner.Run(appHost, _loggerFactory); try { @@ -438,7 +443,7 @@ namespace Jellyfin.Server private static async Task CreateConfiguration(IApplicationPaths appPaths) { const string ResourcePath = "Jellyfin.Server.Resources.Configuration.logging.json"; - string configPath = Path.Combine(appPaths.ConfigurationDirectoryPath, "logging.json"); + string configPath = Path.Combine(appPaths.ConfigurationDirectoryPath, LoggingConfigFile); if (!File.Exists(configPath)) { @@ -460,7 +465,7 @@ namespace Jellyfin.Server return new ConfigurationBuilder() .SetBasePath(appPaths.ConfigurationDirectoryPath) .AddInMemoryCollection(ConfigurationOptions.Configuration) - .AddJsonFile("logging.json", false, true) + .AddJsonFile(LoggingConfigFile, false, true) .AddEnvironmentVariables("JELLYFIN_") .Build(); }