1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-26 17:59:14 +00:00

Added: Handle ctrl-c more gracefully.

Finally fixes #2218
This commit is contained in:
Leonardo Galli 2018-02-19 12:48:56 +01:00
parent c34137c423
commit fa212872ab
4 changed files with 74 additions and 1 deletions

View file

@ -26,6 +26,7 @@ public class NzbDroneServiceFactory : ServiceBase, INzbDroneServiceFactory, IHan
private readonly IBrowserService _browserService;
private readonly IContainer _container;
private readonly Logger _logger;
private CancelHandler _cancelHandler;
public NzbDroneServiceFactory(IConfigFileProvider configFileProvider,
IHostController hostController,
@ -53,7 +54,8 @@ public void Start()
{
if (OsInfo.IsNotWindows)
{
Console.CancelKeyPress += (sender, eventArgs) => LogManager.Configuration = null;
//Console.CancelKeyPress += (sender, eventArgs) => eventArgs.Cancel = true;
//_cancelHandler = new CancelHandler();
}
_runtimeInfo.IsRunning = true;
@ -66,6 +68,7 @@ public void Start()
_browserService.LaunchWebUI();
}
_container.Resolve<IEventAggregator>().PublishEvent(new ApplicationStartedEvent());
}

View file

@ -37,6 +37,8 @@ public static void Start(StartupContext startupContext, IUserAlert userAlert, Ac
var appMode = GetApplicationMode(startupContext);
Start(appMode, startupContext);
_container.Resolve<ICancelHandler>().Attach();
if (startCallback != null)
{

View file

@ -0,0 +1,67 @@
using System;
using NLog;
using NzbDrone.Core.Lifecycle;
namespace Radarr.Host
{
public interface ICancelHandler
{
void Attach();
}
class CancelHandler : ICancelHandler
{
private object _syncRoot;
private volatile bool _cancelInitiated;
private readonly ILifecycleService _lifecycleService;
public CancelHandler(ILifecycleService lifecycleService)
{
_lifecycleService = lifecycleService;
}
public void Attach()
{
Console.CancelKeyPress += HandlerCancelKeyPress;
_syncRoot = new object();
}
private void HandlerCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
// Tell system to ignore the Ctrl+C and not terminate. We'll do that.
e.Cancel = true;
var shouldTerminate = false;
lock (_syncRoot)
{
shouldTerminate = _cancelInitiated;
_cancelInitiated = true;
}
// TODO: Probably should schedule these on the threadpool.
if (shouldTerminate)
{
UngracefulShutdown();
}
else
{
GracefulShutdown();
}
}
private void GracefulShutdown()
{
Console.WriteLine("Shutdown requested, press Ctrl+C again to terminate directly.");
// TODO: Sent ApplicationShutdownRequested event or something like it.
_lifecycleService.Shutdown();
}
private void UngracefulShutdown()
{
Console.WriteLine("Termination requested.");
// TODO: Kill it. Shutdown NLog and invoke Environment.Exit.
LogManager.Configuration = null;
Environment.Exit(0);
}
}
}

View file

@ -113,6 +113,7 @@
</Compile>
<Compile Include="Bootstrap.cs" />
<Compile Include="BrowserService.cs" />
<Compile Include="CancelHandler.cs" />
<Compile Include="IUserAlert.cs" />
<Compile Include="MainAppContainerBuilder.cs" />
<Compile Include="Owin\IHostController.cs" />