jellyfin-server/Jellyfin.Server/Program.cs

301 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
2019-01-12 22:58:58 +00:00
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
using Emby.Server.Implementations;
using Jellyfin.Server.Extensions;
using Jellyfin.Server.Helpers;
using Jellyfin.Server.Implementations;
using MediaBrowser.Common.Configuration;
2023-01-15 20:39:57 +00:00
using MediaBrowser.Controller;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
2019-09-11 17:31:35 +00:00
using Serilog.Extensions.Logging;
2022-02-14 13:39:33 +00:00
using static MediaBrowser.Controller.Extensions.ConfigurationExtensions;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Jellyfin.Server
{
/// <summary>
/// Class containing the entry point of the application.
/// </summary>
public static class Program
{
2020-03-05 17:09:33 +00:00
/// <summary>
/// The name of logging configuration file containing application defaults.
2020-03-05 17:09:33 +00:00
/// </summary>
public const string LoggingConfigFileDefault = "logging.default.json";
/// <summary>
/// The name of the logging configuration file containing the system-specific override settings.
/// </summary>
public const string LoggingConfigFileSystem = "logging.json";
2020-03-05 17:09:33 +00:00
2019-01-12 22:58:58 +00:00
private static readonly ILoggerFactory _loggerFactory = new SerilogLoggerFactory();
2023-01-15 20:39:57 +00:00
private static CancellationTokenSource _tokenSource = new();
private static long _startTimestamp;
private static ILogger _logger = NullLogger.Instance;
private static bool _restartOnShutdown;
/// <summary>
/// The entry point of the application.
/// </summary>
/// <param name="args">The command line arguments passed.</param>
/// <returns><see cref="Task" />.</returns>
2019-02-24 02:16:19 +00:00
public static Task Main(string[] args)
{
static Task ErrorParsingArguments(IEnumerable<Error> errors)
{
Environment.ExitCode = 1;
return Task.CompletedTask;
}
// Parse the command line arguments and either start the app or exit indicating error
2019-02-24 02:16:19 +00:00
return Parser.Default.ParseArguments<StartupOptions>(args)
.MapResult(StartApp, ErrorParsingArguments);
}
/// <summary>
/// Shuts down the application.
/// </summary>
internal static void Shutdown()
2019-02-13 16:19:55 +00:00
{
if (!_tokenSource.IsCancellationRequested)
{
_tokenSource.Cancel();
}
}
/// <summary>
/// Restarts the application.
/// </summary>
internal static void Restart()
2019-02-13 16:19:55 +00:00
{
_restartOnShutdown = true;
Shutdown();
}
private static async Task StartApp(StartupOptions options)
{
2023-01-15 20:39:57 +00:00
_startTimestamp = Stopwatch.GetTimestamp();
// Log all uncaught exceptions to std error
static void UnhandledExceptionToConsole(object sender, UnhandledExceptionEventArgs e) =>
2023-01-15 20:39:57 +00:00
Console.Error.WriteLine("Unhandled Exception\n" + e.ExceptionObject);
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionToConsole;
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
// $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager
Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", appPaths.LogDirectoryPath);
2020-11-24 15:25:32 +00:00
// Enable cl-va P010 interop for tonemapping on Intel VAAPI
Environment.SetEnvironmentVariable("NEOReadDebugKeys", "1");
Environment.SetEnvironmentVariable("EnableExtendedVaFormats", "1");
await StartupHelpers.InitLoggingConfigFile(appPaths).ConfigureAwait(false);
// Create an instance of the application configuration to use for application startup
IConfiguration startupConfig = CreateAppConfiguration(options, appPaths);
StartupHelpers.InitializeLoggingFramework(startupConfig, appPaths);
_logger = _loggerFactory.CreateLogger("Main");
// Log uncaught exceptions to the logging instead of std error
AppDomain.CurrentDomain.UnhandledException -= UnhandledExceptionToConsole;
2021-08-04 12:40:09 +00:00
AppDomain.CurrentDomain.UnhandledException += (_, e)
=> _logger.LogCritical((Exception)e.ExceptionObject, "Unhandled Exception");
2019-01-12 22:58:58 +00:00
// Intercept Ctrl+C and Ctrl+Break
2021-08-04 12:40:09 +00:00
Console.CancelKeyPress += (_, e) =>
2019-01-12 22:58:58 +00:00
{
2019-01-13 00:05:25 +00:00
if (_tokenSource.IsCancellationRequested)
{
return; // Already shutting down
}
2019-02-13 16:19:55 +00:00
2019-01-12 22:58:58 +00:00
e.Cancel = true;
_logger.LogInformation("Ctrl+C, shutting down");
2019-01-13 00:05:25 +00:00
Environment.ExitCode = 128 + 2;
2019-01-12 22:58:58 +00:00
Shutdown();
};
// Register a SIGTERM handler
2021-08-04 12:40:09 +00:00
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
{
2019-01-12 22:58:58 +00:00
if (_tokenSource.IsCancellationRequested)
{
return; // Already shutting down
}
2019-02-13 16:19:55 +00:00
_logger.LogInformation("Received a SIGTERM signal, shutting down");
2019-01-13 00:05:25 +00:00
Environment.ExitCode = 128 + 15;
Shutdown();
};
2019-09-27 21:58:04 +00:00
_logger.LogInformation(
"Jellyfin version: {Version}",
Assembly.GetEntryAssembly()!.GetName().Version!.ToString(3));
ApplicationHost.LogEnvironmentInfo(_logger, appPaths);
2021-11-02 15:02:52 +00:00
// If hosting the web client, validate the client content path
if (startupConfig.HostWebClient())
{
2023-01-15 20:39:57 +00:00
var webContentPath = appPaths.WebPath;
2021-11-02 15:02:52 +00:00
if (!Directory.Exists(webContentPath) || !Directory.EnumerateFiles(webContentPath).Any())
{
_logger.LogError(
"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 " +
"server, you may set the '--nowebclient' command line flag, or set" +
2023-01-15 20:39:57 +00:00
"'{ConfigKey}=false' in your config settings",
2021-11-02 15:02:52 +00:00
webContentPath,
2022-02-14 13:39:33 +00:00
HostWebClientKey);
2021-11-02 15:02:52 +00:00
Environment.ExitCode = 1;
return;
}
}
StartupHelpers.PerformStaticInitialization();
2021-11-24 12:00:12 +00:00
Migrations.MigrationRunner.RunPreStartup(appPaths, _loggerFactory);
2023-01-15 20:39:57 +00:00
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(
appPaths,
_loggerFactory,
options,
2021-11-02 15:02:52 +00:00
startupConfig);
2023-01-15 20:39:57 +00:00
IHost? host = null;
try
{
2023-01-15 20:39:57 +00:00
host = Host.CreateDefaultBuilder()
2023-01-12 16:51:12 +00:00
.ConfigureServices(services => appHost.Init(services))
.ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.ConfigureWebHostBuilder(appHost, startupConfig, appPaths, _logger))
2023-01-12 03:07:41 +00:00
.ConfigureAppConfiguration(config => config.ConfigureAppConfiguration(options, appPaths, startupConfig))
.UseSerilog()
.Build();
2023-01-12 03:07:41 +00:00
// Re-use the host service provider in the app host since ASP.NET doesn't allow a custom service collection.
appHost.ServiceProvider = host.Services;
await appHost.InitializeServices().ConfigureAwait(false);
2020-03-05 17:09:33 +00:00
Migrations.MigrationRunner.Run(appHost, _loggerFactory);
try
{
2023-01-12 03:07:41 +00:00
await host.StartAsync(_tokenSource.Token).ConfigureAwait(false);
2022-10-13 16:19:11 +00:00
if (!OperatingSystem.IsWindows() && startupConfig.UseUnixSocket())
{
var socketPath = StartupHelpers.GetUnixSocketPath(startupConfig, appPaths);
StartupHelpers.SetUnixSocketPermissions(startupConfig, socketPath, _logger);
}
}
2021-09-20 20:27:20 +00:00
catch (Exception ex) when (ex is not TaskCanceledException)
{
2023-01-15 20:39:57 +00:00
_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;
}
2021-02-23 16:30:24 +00:00
await appHost.RunStartupTasksAsync(_tokenSource.Token).ConfigureAwait(false);
2023-01-15 20:39:57 +00:00
_logger.LogInformation("Startup complete {Time:g}", Stopwatch.GetElapsedTime(_startTimestamp));
2019-09-28 22:29:28 +00:00
// Block main thread until shutdown
await Task.Delay(-1, _tokenSource.Token).ConfigureAwait(false);
}
catch (TaskCanceledException)
{
// Don't throw on cancellation
}
catch (Exception ex)
{
2023-01-15 20:39:57 +00:00
_logger.LogCritical(ex, "Error while starting server");
}
finally
{
// Don't throw additional exception if startup failed.
2022-12-05 14:01:13 +00:00
if (appHost.ServiceProvider is not null)
{
_logger.LogInformation("Running query planner optimizations in the database... This might take a while");
// Run before disposing the application
2023-01-16 17:14:44 +00:00
var context = await appHost.ServiceProvider.GetRequiredService<IDbContextFactory<JellyfinDbContext>>().CreateDbContextAsync().ConfigureAwait(false);
await using (context.ConfigureAwait(false))
{
if (context.Database.IsSqlite())
{
await context.Database.ExecuteSqlRawAsync("PRAGMA optimize").ConfigureAwait(false);
}
}
}
2021-05-24 08:48:01 +00:00
await appHost.DisposeAsync().ConfigureAwait(false);
2023-01-15 20:39:57 +00:00
host?.Dispose();
}
}
/// <summary>
/// Create the application configuration.
/// </summary>
/// <param name="commandLineOpts">The command line options passed to the program.</param>
/// <param name="appPaths">The application paths.</param>
/// <returns>The application configuration.</returns>
public static IConfiguration CreateAppConfiguration(StartupOptions commandLineOpts, IApplicationPaths appPaths)
{
return new ConfigurationBuilder()
.ConfigureAppConfiguration(commandLineOpts, appPaths)
.Build();
}
private static IConfigurationBuilder ConfigureAppConfiguration(
this IConfigurationBuilder config,
StartupOptions commandLineOpts,
IApplicationPaths appPaths,
IConfiguration? startupConfig = null)
{
// Use the swagger API page as the default redirect path if not hosting the web client
var inMemoryDefaultConfig = ConfigurationOptions.DefaultConfiguration;
2022-12-05 14:01:13 +00:00
if (startupConfig is not null && !startupConfig.HostWebClient())
{
2022-02-14 13:39:33 +00:00
inMemoryDefaultConfig[DefaultRedirectKey] = "api-docs/swagger";
}
return config
.SetBasePath(appPaths.ConfigurationDirectoryPath)
.AddInMemoryCollection(inMemoryDefaultConfig)
.AddJsonFile(LoggingConfigFileDefault, optional: false, reloadOnChange: true)
.AddJsonFile(LoggingConfigFileSystem, optional: true, reloadOnChange: true)
.AddEnvironmentVariables("JELLYFIN_")
.AddInMemoryCollection(commandLineOpts.ConvertToConfig());
}
}
}