Merge pull request #2523 from JustAMan/logging-migration
Improve migrations so they are more maintainablepull/2535/head
commit
3d563ca3a3
@ -1,92 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server
|
||||
{
|
||||
/// <summary>
|
||||
/// The class that knows how migrate between different Jellyfin versions.
|
||||
/// </summary>
|
||||
internal static class Migrations
|
||||
{
|
||||
private static readonly IUpdater[] _migrations =
|
||||
{
|
||||
new Pre10_5()
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Interface that descibes a migration routine.
|
||||
/// </summary>
|
||||
private 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; }
|
||||
|
||||
/// <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>
|
||||
/// 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)
|
||||
{
|
||||
bool updated = false;
|
||||
var version = host.ServerConfigurationManager.CommonConfiguration.PreviousVersion;
|
||||
|
||||
for (var i = 0; i < _migrations.Length; i++)
|
||||
{
|
||||
var updater = _migrations[i];
|
||||
if (version.CompareTo(updater.Maximum) >= 0)
|
||||
{
|
||||
logger.LogDebug("Skipping updater {0} as current version {1} >= its maximum applicable version {2}", updater, version, updater.Maximum);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (updater.Perform(host, logger, version))
|
||||
{
|
||||
updated = true;
|
||||
}
|
||||
|
||||
version = updater.Maximum;
|
||||
}
|
||||
|
||||
return updated;
|
||||
}
|
||||
|
||||
private class Pre10_5 : IUpdater
|
||||
{
|
||||
public Version Maximum { get => Version.Parse("10.5.0"); }
|
||||
|
||||
public bool Perform(CoreAppHost host, ILogger logger, Version from)
|
||||
{
|
||||
// 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");
|
||||
if (encoding.EnableThrottling)
|
||||
{
|
||||
logger.LogInformation("Disabling transcoding throttling during migration");
|
||||
encoding.EnableThrottling = false;
|
||||
|
||||
host.ServerConfigurationManager.SaveConfiguration("encoding", encoding);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Migrations
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface that descibes a migration routine.
|
||||
/// </summary>
|
||||
internal interface IUpdater
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name of the migration, must be unique.
|
||||
/// </summary>
|
||||
public abstract string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Execute the migration routine.
|
||||
/// </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 the list of applied migration routine names.
|
||||
/// </summary>
|
||||
public string[] Applied { get; set; }
|
||||
#pragma warning restore CA1819 // Properties should not return arrays
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Migrations
|
||||
{
|
||||
/// <summary>
|
||||
/// The class that knows which migrations to apply and how to apply them.
|
||||
/// </summary>
|
||||
public sealed class MigrationRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// The list of known migrations, in order of applicability.
|
||||
/// </summary>
|
||||
internal static readonly IUpdater[] Migrations =
|
||||
{
|
||||
new Routines.DisableTranscodingThrottling()
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Run all needed migrations.
|
||||
/// </summary>
|
||||
/// <param name="host">CoreAppHost that hosts current version.</param>
|
||||
/// <param name="loggerFactory">Factory for making the logger.</param>
|
||||
public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger<MigrationRunner>();
|
||||
var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);
|
||||
|
||||
if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted && migrationOptions.Applied.Length == 0)
|
||||
{
|
||||
// If startup wizard is not finished, this is a fresh install.
|
||||
// Don't run any migrations, just mark all of them as applied.
|
||||
logger.LogInformation("Marking all known migrations as applied because this is fresh install");
|
||||
migrationOptions.Applied = Migrations.Select(m => m.Name).ToArray();
|
||||
host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
|
||||
return;
|
||||
}
|
||||
|
||||
var applied = migrationOptions.Applied.ToList();
|
||||
|
||||
for (var i = 0; i < Migrations.Length; i++)
|
||||
{
|
||||
var updater = Migrations[i];
|
||||
if (applied.Contains(updater.Name))
|
||||
{
|
||||
logger.LogDebug("Skipping migration '{Name}' since it is already applied", updater.Name);
|
||||
continue;
|
||||
}
|
||||
|
||||
logger.LogInformation("Applying migration '{Name}'", updater.Name);
|
||||
try
|
||||
{
|
||||
updater.Perform(host, logger);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Could not apply migration '{Name}'", updater.Name);
|
||||
throw;
|
||||
}
|
||||
|
||||
logger.LogInformation("Migration '{Name}' applied successfully", updater.Name);
|
||||
applied.Add(updater.Name);
|
||||
}
|
||||
|
||||
if (applied.Count > migrationOptions.Applied.Length)
|
||||
{
|
||||
logger.LogInformation("Some migrations were run, saving the state");
|
||||
migrationOptions.Applied = applied.ToArray();
|
||||
host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
|
||||
namespace Jellyfin.Server.Migrations
|
||||
{
|
||||
/// <summary>
|
||||
/// A factory that can find a persistent file of the migration configuration, which lists all applied migrations.
|
||||
/// </summary>
|
||||
public class MigrationsFactory : IConfigurationFactory
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<ConfigurationStore> GetConfigurations()
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
new MigrationsListStore()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
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>
|
||||
/// The name of the configuration in the storage.
|
||||
/// </summary>
|
||||
public static readonly string StoreKey = "migrations";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MigrationsListStore"/> class.
|
||||
/// </summary>
|
||||
public MigrationsListStore()
|
||||
{
|
||||
ConfigurationType = typeof(MigrationOptions);
|
||||
Key = StoreKey;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
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.Routines
|
||||
{
|
||||
/// <summary>
|
||||
/// Disable transcode throttling for all installations since it is currently broken for certain video formats.
|
||||
/// </summary>
|
||||
internal class DisableTranscodingThrottling : IUpdater
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public string Name => "DisableTranscodingThrottling";
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Perform(CoreAppHost host, ILogger logger)
|
||||
{
|
||||
// Set EnableThrottling to false since it wasn't used before and may introduce issues
|
||||
var encoding = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<EncodingOptions>("encoding");
|
||||
if (encoding.EnableThrottling)
|
||||
{
|
||||
logger.LogInformation("Disabling transcoding throttling during migration");
|
||||
encoding.EnableThrottling = false;
|
||||
|
||||
host.ServerConfigurationManager.SaveConfiguration("encoding", encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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.Routines
|
||||
{
|
||||
/// <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");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue