From aa1a76fe7281ea7a27a115ca00f5ddc6a0c94b8e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 13 Jan 2014 17:35:16 -0800 Subject: [PATCH] Restart for Windows --- .../EnvironmentInfo/RuntimeInfo.cs | 4 +- .../EnvironmentInfo/StartupContext.cs | 2 +- .../Lifecycle/ApplicationRestartRequested.cs | 9 ----- .../Lifecycle/ApplicationShutdownRequested.cs | 9 +++++ .../Lifecycle/LifestyleService.cs | 39 +++++++++++++++---- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 - src/NzbDrone.Host/ApplicationServer.cs | 2 +- src/NzbDrone.Host/Bootstrap.cs | 12 ++++-- 8 files changed, 53 insertions(+), 25 deletions(-) delete mode 100644 src/NzbDrone.Core/Lifecycle/ApplicationRestartRequested.cs diff --git a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs index 4937ea1cbc..aec416e61e 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs @@ -4,10 +4,10 @@ using System.Security.Principal; using System.ServiceProcess; using NLog; +using NzbDrone.Common.Processes; namespace NzbDrone.Common.EnvironmentInfo { - public interface IRuntimeInfo { bool IsUserInteractive { get; } @@ -67,7 +67,7 @@ public bool IsConsole { return (OsInfo.IsWindows && IsUserInteractive && - ProcessName.Equals("NzbDrone.Console.exe", StringComparison.InvariantCultureIgnoreCase)) || + ProcessName.Equals(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase)) || OsInfo.IsLinux; } } diff --git a/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs b/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs index 0702fc8614..3331b39e46 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs @@ -17,6 +17,7 @@ public class StartupContext : IStartupContext internal const string INSTALL_SERVICE = "i"; internal const string UNINSTALL_SERVICE = "u"; public const string HELP = "?"; + public const string TERMINATE = "terminateexisting"; public StartupContext(params string[] args) { @@ -58,6 +59,5 @@ public bool UninstallService return Flags.Contains(UNINSTALL_SERVICE); } } - } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Lifecycle/ApplicationRestartRequested.cs b/src/NzbDrone.Core/Lifecycle/ApplicationRestartRequested.cs deleted file mode 100644 index 7aa08bb1f3..0000000000 --- a/src/NzbDrone.Core/Lifecycle/ApplicationRestartRequested.cs +++ /dev/null @@ -1,9 +0,0 @@ -using NzbDrone.Common.Messaging; - -namespace NzbDrone.Core.Lifecycle -{ - public class ApplicationRestartRequested : IEvent - { - - } -} \ No newline at end of file diff --git a/src/NzbDrone.Core/Lifecycle/ApplicationShutdownRequested.cs b/src/NzbDrone.Core/Lifecycle/ApplicationShutdownRequested.cs index 6c343546ef..50446ed1d6 100644 --- a/src/NzbDrone.Core/Lifecycle/ApplicationShutdownRequested.cs +++ b/src/NzbDrone.Core/Lifecycle/ApplicationShutdownRequested.cs @@ -4,6 +4,15 @@ namespace NzbDrone.Core.Lifecycle { public class ApplicationShutdownRequested : IEvent { + public bool Restarting { get; set; } + public ApplicationShutdownRequested() + { + } + + public ApplicationShutdownRequested(bool restarting) + { + Restarting = restarting; + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Lifecycle/LifestyleService.cs b/src/NzbDrone.Core/Lifecycle/LifestyleService.cs index d08aee7674..bc09bf8774 100644 --- a/src/NzbDrone.Core/Lifecycle/LifestyleService.cs +++ b/src/NzbDrone.Core/Lifecycle/LifestyleService.cs @@ -1,4 +1,5 @@ -using NzbDrone.Common; +using System.IO; +using NzbDrone.Common; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Processes; using NzbDrone.Core.Lifecycle.Commands; @@ -12,42 +13,66 @@ public class LifestyleService: IExecute, IExecute - diff --git a/src/NzbDrone.Host/ApplicationServer.cs b/src/NzbDrone.Host/ApplicationServer.cs index b506e64fd5..4d1af5f068 100644 --- a/src/NzbDrone.Host/ApplicationServer.cs +++ b/src/NzbDrone.Host/ApplicationServer.cs @@ -75,7 +75,7 @@ private void Shutdown() public void Handle(ApplicationShutdownRequested message) { - if (!_runtimeInfo.IsWindowsService) + if (!_runtimeInfo.IsWindowsService && !message.Restarting) { Shutdown(); } diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index 87448075b5..2890d4ec36 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -36,7 +36,7 @@ public static void Start(StartupContext startupContext, IUserAlert userAlert, Ac var appMode = GetApplicationMode(startupContext); - Start(appMode); + Start(appMode, startupContext); if (startCallback != null) { @@ -54,11 +54,11 @@ public static void Start(StartupContext startupContext, IUserAlert userAlert, Ac } } - private static void Start(ApplicationModes applicationModes) + private static void Start(ApplicationModes applicationModes, StartupContext startupContext) { if (!IsInUtilityMode(applicationModes)) { - EnsureSingleInstance(applicationModes == ApplicationModes.Service); + EnsureSingleInstance(applicationModes == ApplicationModes.Service, startupContext); } DbFactory.RegisterDatabase(_container); @@ -80,7 +80,7 @@ private static void SpinToExit(ApplicationModes applicationModes) } } - private static void EnsureSingleInstance(bool isService) + private static void EnsureSingleInstance(bool isService, StartupContext startupContext) { var instancePolicy = _container.Resolve(); @@ -88,6 +88,10 @@ private static void EnsureSingleInstance(bool isService) { instancePolicy.KillAllOtherInstance(); } + else if (startupContext.Flags.Contains(StartupContext.TERMINATE)) + { + instancePolicy.KillAllOtherInstance(); + } else { instancePolicy.PreventStartIfAlreadyRunning();