Enable in-process restarting
This commit is contained in:
parent
f8ca71ee15
commit
dc85d86ea1
|
@ -193,11 +193,6 @@ namespace Emby.Server.Implementations
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private string PublishedServerUrl => _startupConfig[AddressOverrideKey];
|
private string PublishedServerUrl => _startupConfig[AddressOverrideKey];
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether this instance can self restart.
|
|
||||||
/// </summary>
|
|
||||||
public bool CanSelfRestart => _startupOptions.RestartPath is not null;
|
|
||||||
|
|
||||||
public bool CoreStartupHasCompleted { get; private set; }
|
public bool CoreStartupHasCompleted { get; private set; }
|
||||||
|
|
||||||
public virtual bool CanLaunchWebBrowser
|
public virtual bool CanLaunchWebBrowser
|
||||||
|
@ -935,17 +930,13 @@ namespace Emby.Server.Implementations
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Restart()
|
public void Restart()
|
||||||
{
|
{
|
||||||
if (!CanSelfRestart)
|
|
||||||
{
|
|
||||||
throw new PlatformNotSupportedException("The server is unable to self-restart. Please restart manually.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsShuttingDown)
|
if (IsShuttingDown)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
IsShuttingDown = true;
|
IsShuttingDown = true;
|
||||||
|
_pluginManager.UnloadAssemblies();
|
||||||
|
|
||||||
Task.Run(async () =>
|
Task.Run(async () =>
|
||||||
{
|
{
|
||||||
|
@ -1047,7 +1038,7 @@ namespace Emby.Server.Implementations
|
||||||
CachePath = ApplicationPaths.CachePath,
|
CachePath = ApplicationPaths.CachePath,
|
||||||
OperatingSystem = MediaBrowser.Common.System.OperatingSystem.Id.ToString(),
|
OperatingSystem = MediaBrowser.Common.System.OperatingSystem.Id.ToString(),
|
||||||
OperatingSystemDisplayName = MediaBrowser.Common.System.OperatingSystem.Name,
|
OperatingSystemDisplayName = MediaBrowser.Common.System.OperatingSystem.Name,
|
||||||
CanSelfRestart = CanSelfRestart,
|
CanSelfRestart = true,
|
||||||
CanLaunchWebBrowser = CanLaunchWebBrowser,
|
CanLaunchWebBrowser = CanLaunchWebBrowser,
|
||||||
TranscodingTempPath = ConfigurationManager.GetTranscodePath(),
|
TranscodingTempPath = ConfigurationManager.GetTranscodePath(),
|
||||||
ServerName = FriendlyName,
|
ServerName = FriendlyName,
|
||||||
|
|
|
@ -20,16 +20,6 @@ namespace Emby.Server.Implementations
|
||||||
/// </summary>
|
/// </summary>
|
||||||
string? PackageName { get; }
|
string? PackageName { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the value of the --restartpath command line option.
|
|
||||||
/// </summary>
|
|
||||||
string? RestartPath { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the value of the --restartargs command line option.
|
|
||||||
/// </summary>
|
|
||||||
string? RestartArgs { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the value of the --published-server-url command line option.
|
/// Gets the value of the --published-server-url command line option.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.Loader;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -30,6 +31,7 @@ namespace Emby.Server.Implementations.Plugins
|
||||||
{
|
{
|
||||||
private readonly string _pluginsPath;
|
private readonly string _pluginsPath;
|
||||||
private readonly Version _appVersion;
|
private readonly Version _appVersion;
|
||||||
|
private readonly AssemblyLoadContext _assemblyLoadContext;
|
||||||
private readonly JsonSerializerOptions _jsonOptions;
|
private readonly JsonSerializerOptions _jsonOptions;
|
||||||
private readonly ILogger<PluginManager> _logger;
|
private readonly ILogger<PluginManager> _logger;
|
||||||
private readonly IApplicationHost _appHost;
|
private readonly IApplicationHost _appHost;
|
||||||
|
@ -76,6 +78,8 @@ namespace Emby.Server.Implementations.Plugins
|
||||||
_appHost = appHost;
|
_appHost = appHost;
|
||||||
_minimumVersion = new Version(0, 0, 0, 1);
|
_minimumVersion = new Version(0, 0, 0, 1);
|
||||||
_plugins = Directory.Exists(_pluginsPath) ? DiscoverPlugins().ToList() : new List<LocalPlugin>();
|
_plugins = Directory.Exists(_pluginsPath) ? DiscoverPlugins().ToList() : new List<LocalPlugin>();
|
||||||
|
|
||||||
|
_assemblyLoadContext = new AssemblyLoadContext("PluginContext", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IHttpClientFactory HttpClientFactory
|
private IHttpClientFactory HttpClientFactory
|
||||||
|
@ -124,7 +128,7 @@ namespace Emby.Server.Implementations.Plugins
|
||||||
Assembly assembly;
|
Assembly assembly;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
assembly = Assembly.LoadFrom(file);
|
assembly = _assemblyLoadContext.LoadFromAssemblyPath(file);
|
||||||
|
|
||||||
// Load all required types to verify that the plugin will load
|
// Load all required types to verify that the plugin will load
|
||||||
assembly.GetTypes();
|
assembly.GetTypes();
|
||||||
|
@ -156,6 +160,12 @@ namespace Emby.Server.Implementations.Plugins
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void UnloadAssemblies()
|
||||||
|
{
|
||||||
|
_assemblyLoadContext.Unload();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates all the plugin instances.
|
/// Creates all the plugin instances.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -12,6 +12,7 @@ using Jellyfin.Server.Extensions;
|
||||||
using Jellyfin.Server.Helpers;
|
using Jellyfin.Server.Helpers;
|
||||||
using Jellyfin.Server.Implementations;
|
using Jellyfin.Server.Implementations;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
|
using MediaBrowser.Controller;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
@ -40,8 +41,9 @@ namespace Jellyfin.Server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const string LoggingConfigFileSystem = "logging.json";
|
public const string LoggingConfigFileSystem = "logging.json";
|
||||||
|
|
||||||
private static readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
|
||||||
private static readonly ILoggerFactory _loggerFactory = new SerilogLoggerFactory();
|
private static readonly ILoggerFactory _loggerFactory = new SerilogLoggerFactory();
|
||||||
|
private static CancellationTokenSource _tokenSource = new();
|
||||||
|
private static long _startTimestamp;
|
||||||
private static ILogger _logger = NullLogger.Instance;
|
private static ILogger _logger = NullLogger.Instance;
|
||||||
private static bool _restartOnShutdown;
|
private static bool _restartOnShutdown;
|
||||||
|
|
||||||
|
@ -86,11 +88,11 @@ namespace Jellyfin.Server
|
||||||
|
|
||||||
private static async Task StartApp(StartupOptions options)
|
private static async Task StartApp(StartupOptions options)
|
||||||
{
|
{
|
||||||
var startTimestamp = Stopwatch.GetTimestamp();
|
_startTimestamp = Stopwatch.GetTimestamp();
|
||||||
|
|
||||||
// Log all uncaught exceptions to std error
|
// Log all uncaught exceptions to std error
|
||||||
static void UnhandledExceptionToConsole(object sender, UnhandledExceptionEventArgs e) =>
|
static void UnhandledExceptionToConsole(object sender, UnhandledExceptionEventArgs e) =>
|
||||||
Console.Error.WriteLine("Unhandled Exception\n" + e.ExceptionObject.ToString());
|
Console.Error.WriteLine("Unhandled Exception\n" + e.ExceptionObject);
|
||||||
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionToConsole;
|
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionToConsole;
|
||||||
|
|
||||||
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
|
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
|
||||||
|
@ -151,14 +153,14 @@ namespace Jellyfin.Server
|
||||||
// If hosting the web client, validate the client content path
|
// If hosting the web client, validate the client content path
|
||||||
if (startupConfig.HostWebClient())
|
if (startupConfig.HostWebClient())
|
||||||
{
|
{
|
||||||
string? webContentPath = appPaths.WebPath;
|
var webContentPath = appPaths.WebPath;
|
||||||
if (!Directory.Exists(webContentPath) || !Directory.EnumerateFiles(webContentPath).Any())
|
if (!Directory.Exists(webContentPath) || !Directory.EnumerateFiles(webContentPath).Any())
|
||||||
{
|
{
|
||||||
_logger.LogError(
|
_logger.LogError(
|
||||||
"The server is expected to host the web client, but the provided content directory is either " +
|
"The server is expected to host the web client, but the provided content directory is either " +
|
||||||
"invalid or empty: {WebContentPath}. If you do not want to host the web client with the " +
|
"invalid or empty: {WebContentPath}. If you do not want to host the web client with the " +
|
||||||
"server, you may set the '--nowebclient' command line flag, or set" +
|
"server, you may set the '--nowebclient' command line flag, or set" +
|
||||||
"'{ConfigKey}=false' in your config settings.",
|
"'{ConfigKey}=false' in your config settings",
|
||||||
webContentPath,
|
webContentPath,
|
||||||
HostWebClientKey);
|
HostWebClientKey);
|
||||||
Environment.ExitCode = 1;
|
Environment.ExitCode = 1;
|
||||||
|
@ -169,15 +171,31 @@ namespace Jellyfin.Server
|
||||||
StartupHelpers.PerformStaticInitialization();
|
StartupHelpers.PerformStaticInitialization();
|
||||||
Migrations.MigrationRunner.RunPreStartup(appPaths, _loggerFactory);
|
Migrations.MigrationRunner.RunPreStartup(appPaths, _loggerFactory);
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
_restartOnShutdown = false;
|
||||||
|
await StartServer(appPaths, options, startupConfig).ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (_restartOnShutdown)
|
||||||
|
{
|
||||||
|
_tokenSource = new CancellationTokenSource();
|
||||||
|
_startTimestamp = Stopwatch.GetTimestamp();
|
||||||
|
}
|
||||||
|
} while (_restartOnShutdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task StartServer(IServerApplicationPaths appPaths, StartupOptions options, IConfiguration startupConfig)
|
||||||
|
{
|
||||||
var appHost = new CoreAppHost(
|
var appHost = new CoreAppHost(
|
||||||
appPaths,
|
appPaths,
|
||||||
_loggerFactory,
|
_loggerFactory,
|
||||||
options,
|
options,
|
||||||
startupConfig);
|
startupConfig);
|
||||||
|
|
||||||
|
IHost? host = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var host = Host.CreateDefaultBuilder()
|
host = Host.CreateDefaultBuilder()
|
||||||
.ConfigureServices(services => appHost.Init(services))
|
.ConfigureServices(services => appHost.Init(services))
|
||||||
.ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.ConfigureWebHostBuilder(appHost, startupConfig, appPaths, _logger))
|
.ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.ConfigureWebHostBuilder(appHost, startupConfig, appPaths, _logger))
|
||||||
.ConfigureAppConfiguration(config => config.ConfigureAppConfiguration(options, appPaths, startupConfig))
|
.ConfigureAppConfiguration(config => config.ConfigureAppConfiguration(options, appPaths, startupConfig))
|
||||||
|
@ -203,13 +221,13 @@ namespace Jellyfin.Server
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (ex is not TaskCanceledException)
|
catch (Exception ex) when (ex is not TaskCanceledException)
|
||||||
{
|
{
|
||||||
_logger.LogError("Kestrel failed to start! This is most likely due to an invalid address or port bind - correct your bind configuration in network.xml and try again.");
|
_logger.LogError("Kestrel failed to start! This is most likely due to an invalid address or port bind - correct your bind configuration in network.xml and try again");
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
await appHost.RunStartupTasksAsync(_tokenSource.Token).ConfigureAwait(false);
|
await appHost.RunStartupTasksAsync(_tokenSource.Token).ConfigureAwait(false);
|
||||||
|
|
||||||
_logger.LogInformation("Startup complete {Time:g}", Stopwatch.GetElapsedTime(startTimestamp));
|
_logger.LogInformation("Startup complete {Time:g}", Stopwatch.GetElapsedTime(_startTimestamp));
|
||||||
|
|
||||||
// Block main thread until shutdown
|
// Block main thread until shutdown
|
||||||
await Task.Delay(-1, _tokenSource.Token).ConfigureAwait(false);
|
await Task.Delay(-1, _tokenSource.Token).ConfigureAwait(false);
|
||||||
|
@ -220,7 +238,7 @@ namespace Jellyfin.Server
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogCritical(ex, "Error while starting server.");
|
_logger.LogCritical(ex, "Error while starting server");
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
@ -240,11 +258,7 @@ namespace Jellyfin.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
await appHost.DisposeAsync().ConfigureAwait(false);
|
await appHost.DisposeAsync().ConfigureAwait(false);
|
||||||
}
|
host?.Dispose();
|
||||||
|
|
||||||
if (_restartOnShutdown)
|
|
||||||
{
|
|
||||||
StartNewInstance(options);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,44 +296,5 @@ namespace Jellyfin.Server
|
||||||
.AddEnvironmentVariables("JELLYFIN_")
|
.AddEnvironmentVariables("JELLYFIN_")
|
||||||
.AddInMemoryCollection(commandLineOpts.ConvertToConfig());
|
.AddInMemoryCollection(commandLineOpts.ConvertToConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void StartNewInstance(StartupOptions options)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Starting new instance");
|
|
||||||
|
|
||||||
var module = options.RestartPath;
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(module))
|
|
||||||
{
|
|
||||||
module = Environment.GetCommandLineArgs()[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
string commandLineArgsString;
|
|
||||||
if (options.RestartArgs is not null)
|
|
||||||
{
|
|
||||||
commandLineArgsString = options.RestartArgs;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
commandLineArgsString = string.Join(
|
|
||||||
' ',
|
|
||||||
Environment.GetCommandLineArgs().Skip(1).Select(NormalizeCommandLineArgument));
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogInformation("Executable: {0}", module);
|
|
||||||
_logger.LogInformation("Arguments: {0}", commandLineArgsString);
|
|
||||||
|
|
||||||
Process.Start(module, commandLineArgsString);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string NormalizeCommandLineArgument(string arg)
|
|
||||||
{
|
|
||||||
if (!arg.Contains(' ', StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
return arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
return "\"" + arg + "\"";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,14 +63,6 @@ namespace Jellyfin.Server
|
||||||
[Option("package-name", Required = false, HelpText = "Used when packaging Jellyfin (example, synology).")]
|
[Option("package-name", Required = false, HelpText = "Used when packaging Jellyfin (example, synology).")]
|
||||||
public string? PackageName { get; set; }
|
public string? PackageName { get; set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
[Option("restartpath", Required = false, HelpText = "Path to restart script.")]
|
|
||||||
public string? RestartPath { get; set; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
[Option("restartargs", Required = false, HelpText = "Arguments for restart script.")]
|
|
||||||
public string? RestartArgs { get; set; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[Option("published-server-url", Required = false, HelpText = "Jellyfin Server URL to publish via auto discover process")]
|
[Option("published-server-url", Required = false, HelpText = "Jellyfin Server URL to publish via auto discover process")]
|
||||||
public string? PublishedServerUrl { get; set; }
|
public string? PublishedServerUrl { get; set; }
|
||||||
|
|
|
@ -47,12 +47,6 @@ namespace MediaBrowser.Common
|
||||||
/// <value><c>true</c> if this instance is shutting down; otherwise, <c>false</c>.</value>
|
/// <value><c>true</c> if this instance is shutting down; otherwise, <c>false</c>.</value>
|
||||||
bool IsShuttingDown { get; }
|
bool IsShuttingDown { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether this instance can self restart.
|
|
||||||
/// </summary>
|
|
||||||
/// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value>
|
|
||||||
bool CanSelfRestart { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the application version.
|
/// Gets the application version.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -29,6 +29,11 @@ namespace MediaBrowser.Common.Plugins
|
||||||
/// <returns>An IEnumerable{Assembly}.</returns>
|
/// <returns>An IEnumerable{Assembly}.</returns>
|
||||||
IEnumerable<Assembly> LoadAssemblies();
|
IEnumerable<Assembly> LoadAssemblies();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unloads all of the assemblies.
|
||||||
|
/// </summary>
|
||||||
|
void UnloadAssemblies();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers the plugin's services with the DI.
|
/// Registers the plugin's services with the DI.
|
||||||
/// Note: DI is not yet instantiated yet.
|
/// Note: DI is not yet instantiated yet.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user