mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-04-24 14:08:44 -04:00
Merge pull request #567 from Bond-009/term
Shutdown gracefully when recieving a termination signal
This commit is contained in:
commit
2b1e3aa45f
1 changed files with 346 additions and 313 deletions
|
@ -6,6 +6,7 @@ using System.Net;
|
|||
using System.Net.Security;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Emby.Drawing;
|
||||
using Emby.Drawing.Skia;
|
||||
|
@ -29,12 +30,12 @@ namespace Jellyfin.Server
|
|||
{
|
||||
public static class Program
|
||||
{
|
||||
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
||||
private static ILoggerFactory _loggerFactory;
|
||||
private static readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
||||
private static readonly ILoggerFactory _loggerFactory = new SerilogLoggerFactory();
|
||||
private static ILogger _logger;
|
||||
private static bool _restartOnShutdown;
|
||||
|
||||
public static async Task<int> Main(string[] args)
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
StartupOptions options = new StartupOptions(args);
|
||||
Version version = Assembly.GetEntryAssembly().GetName().Version;
|
||||
|
@ -42,19 +43,42 @@ namespace Jellyfin.Server
|
|||
if (options.ContainsOption("-v") || options.ContainsOption("--version"))
|
||||
{
|
||||
Console.WriteLine(version.ToString());
|
||||
return 0;
|
||||
}
|
||||
|
||||
ServerApplicationPaths appPaths = createApplicationPaths(options);
|
||||
// $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager
|
||||
Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", appPaths.LogDirectoryPath);
|
||||
await createLogger(appPaths);
|
||||
_loggerFactory = new SerilogLoggerFactory();
|
||||
_logger = _loggerFactory.CreateLogger("Main");
|
||||
|
||||
AppDomain.CurrentDomain.UnhandledException += (sender, e)
|
||||
=> _logger.LogCritical((Exception)e.ExceptionObject, "Unhandled Exception");
|
||||
|
||||
// Intercept Ctrl+C and Ctrl+Break
|
||||
Console.CancelKeyPress += (sender, e) =>
|
||||
{
|
||||
if (_tokenSource.IsCancellationRequested)
|
||||
{
|
||||
return; // Already shutting down
|
||||
}
|
||||
e.Cancel = true;
|
||||
_logger.LogInformation("Ctrl+C, shutting down");
|
||||
Environment.ExitCode = 128 + 2;
|
||||
Shutdown();
|
||||
};
|
||||
|
||||
// Register a SIGTERM handler
|
||||
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
|
||||
{
|
||||
if (_tokenSource.IsCancellationRequested)
|
||||
{
|
||||
return; // Already shutting down
|
||||
}
|
||||
_logger.LogInformation("Received a SIGTERM signal, shutting down");
|
||||
Environment.ExitCode = 128 + 15;
|
||||
Shutdown();
|
||||
};
|
||||
|
||||
_logger.LogInformation("Jellyfin version: {Version}", version);
|
||||
|
||||
EnvironmentInfo environmentInfo = new EnvironmentInfo(getOperatingSystem());
|
||||
|
@ -86,8 +110,16 @@ namespace Jellyfin.Server
|
|||
await appHost.RunStartupTasks();
|
||||
|
||||
// TODO: read input for a stop command
|
||||
// Block main thread until shutdown
|
||||
await ApplicationTaskCompletionSource.Task;
|
||||
|
||||
try
|
||||
{
|
||||
// Block main thread until shutdown
|
||||
await Task.Delay(-1, _tokenSource.Token);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
// Don't throw on cancellation
|
||||
}
|
||||
|
||||
_logger.LogInformation("Disposing app host");
|
||||
}
|
||||
|
@ -96,8 +128,6 @@ namespace Jellyfin.Server
|
|||
{
|
||||
StartNewInstance(options);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static ServerApplicationPaths createApplicationPaths(StartupOptions options)
|
||||
|
@ -258,7 +288,10 @@ namespace Jellyfin.Server
|
|||
|
||||
public static void Shutdown()
|
||||
{
|
||||
ApplicationTaskCompletionSource.SetResult(true);
|
||||
if (!_tokenSource.IsCancellationRequested)
|
||||
{
|
||||
_tokenSource.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Restart()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue