Improve migration logic

pull/2523/head
Vasily 5 years ago
parent 66e11879ef
commit ecaa7f8014

@ -57,33 +57,5 @@ namespace Jellyfin.Server
/// <inheritdoc />
protected override void ShutdownInternal() => Program.Shutdown();
/// <summary>
/// Runs the migration routines if necessary.
/// </summary>
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;
}
}
}
}

@ -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
/// <summary>
/// Updater that takes care of bringing configuration up to 10.5.0 standards.
/// </summary>
internal class Pre_10_5 : IUpdater
internal class DisableTranscodingThrottling : IUpdater
{
/// <inheritdoc/>
public Version Maximum { get => Version.Parse("10.5.0"); }
public string Name => "DisableTranscodingThrottling";
/// <inheritdoc/>
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<EncodingOptions>("encoding");
@ -24,10 +26,7 @@ namespace Jellyfin.Server.Migrations
encoding.EnableThrottling = false;
host.ServerConfigurationManager.SaveConfiguration("encoding", encoding);
return true;
}
return false;
}
}
}

@ -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
{
/// <summary>
/// Updater that takes care of bringing configuration up to 10.5.0 standards.
/// </summary>
internal class DisableZealousLogging : IUpdater
{
/// <inheritdoc/>
public string Name => "DisableZealousLogging";
/// <inheritdoc/>
// 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");
}
}
}

@ -3,24 +3,21 @@ using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Migrations
{
/// <summary>
/// Interface that descibes a migration routine.
/// </summary>
internal interface IUpdater
{
/// <summary>
/// Interface that descibes a migration routine.
/// Gets the name of the migration, must be unique.
/// </summary>
internal interface IUpdater
{
/// <summary>
/// Gets maximum version this Updater applies to.
/// If current version is greater or equal to it, skip the updater.
/// </summary>
public abstract Version Maximum { get; }
public abstract string Name { get; }
/// <summary>
/// Execute the migration from version "from".
/// </summary>
/// <param name="host">Host that hosts current version.</param>
/// <param name="logger">Host logger.</param>
/// <param name="from">Version to migrate from.</param>
/// <returns>Whether configuration was changed.</returns>
public abstract bool Perform(CoreAppHost host, ILogger logger, Version from);
}
/// <summary>
/// Execute the migration from version "from".
/// </summary>
/// <param name="host">Host that hosts current version.</param>
/// <param name="logger">Host logger.</param>
public abstract void Perform(CoreAppHost host, ILogger logger);
}
}

@ -0,0 +1,23 @@
namespace Jellyfin.Server.Migrations
{
/// <summary>
/// Configuration part that holds all migrations that were applied.
/// </summary>
public class MigrationOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="MigrationOptions"/> class.
/// </summary>
public MigrationOptions()
{
Applied = System.Array.Empty<string>();
}
#pragma warning disable CA1819 // Properties should not return arrays
/// <summary>
/// Gets or sets he list of applied migration routine names.
/// </summary>
public string[] Applied { get; set; }
#pragma warning restore CA1819 // Properties should not return arrays
}
}

@ -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
/// <summary>
/// The class that knows how migrate between different Jellyfin versions.
/// </summary>
public static class MigrationRunner
public sealed class MigrationRunner
{
private static readonly IUpdater[] _migrations =
/// <summary>
/// The list of known migrations, in order of applicability.
/// </summary>
internal static readonly IUpdater[] Migrations =
{
new Pre_10_5()
new DisableTranscodingThrottling(),
new DisableZealousLogging()
};
/// <summary>
/// Run all needed migrations.
/// </summary>
/// <param name="host">CoreAppHost that hosts current version.</param>
/// <param name="logger">AppHost logger.</param>
/// <returns>Whether anything was changed.</returns>
public static bool Run(CoreAppHost host, ILogger logger)
/// <param name="loggerFactory">Factory for making the logger.</param>
public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
{
bool updated = false;
var version = host.ServerConfigurationManager.CommonConfiguration.PreviousVersion;
var logger = loggerFactory.CreateLogger<MigrationRunner>();
var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>("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);
}
}
}
}

@ -0,0 +1,20 @@
using System.Collections.Generic;
using MediaBrowser.Common.Configuration;
namespace Jellyfin.Server.Migrations
{
/// <summary>
/// A factory that teachs Jellyfin how to find a peristent file which lists all applied migrations.
/// </summary>
public class MigrationsFactory : IConfigurationFactory
{
/// <inheritdoc/>
public IEnumerable<ConfigurationStore> GetConfigurations()
{
return new[]
{
new MigrationsListStore()
};
}
}
}

@ -0,0 +1,19 @@
using MediaBrowser.Common.Configuration;
namespace Jellyfin.Server.Migrations
{
/// <summary>
/// A configuration that lists all the migration routines that were applied.
/// </summary>
public class MigrationsListStore : ConfigurationStore
{
/// <summary>
/// Initializes a new instance of the <see cref="MigrationsListStore"/> class.
/// </summary>
public MigrationsListStore()
{
ConfigurationType = typeof(MigrationOptions);
Key = "migrations";
}
}
}

@ -38,6 +38,11 @@ namespace Jellyfin.Server
/// </summary>
public static class Program
{
/// <summary>
/// The name of logging configuration file.
/// </summary>
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<IConfiguration> 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();
}

Loading…
Cancel
Save