diff --git a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs index 4937ea1cb..aec416e61 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs @@ -4,10 +4,10 @@ using System.IO; 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 @@ namespace NzbDrone.Common.EnvironmentInfo { 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 0702fc861..3331b39e4 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs @@ -17,6 +17,7 @@ namespace NzbDrone.Common.EnvironmentInfo 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 @@ namespace NzbDrone.Common.EnvironmentInfo 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 7aa08bb1f..000000000 --- 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 6c343546e..50446ed1d 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 d08aee767..bc09bf877 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 @@ namespace NzbDrone.Core.Lifecycle { private readonly IEventAggregator _eventAggregator; private readonly IRuntimeInfo _runtimeInfo; + private readonly IAppFolderInfo _appFolderInfo; private readonly IServiceProvider _serviceProvider; private readonly IProcessProvider _processProvider; public LifestyleService(IEventAggregator eventAggregator, IRuntimeInfo runtimeInfo, + IAppFolderInfo appFolderInfo, IServiceProvider serviceProvider, IProcessProvider processProvider) { _eventAggregator = eventAggregator; _runtimeInfo = runtimeInfo; + _appFolderInfo = appFolderInfo; _serviceProvider = serviceProvider; _processProvider = processProvider; } public void Execute(ShutdownCommand message) { + _eventAggregator.PublishEvent(new ApplicationShutdownRequested()); + if (_runtimeInfo.IsWindowsService) { _serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME); } - - else - { - _eventAggregator.PublishEvent(new ApplicationShutdownRequested()); - } } public void Execute(RestartCommand message) { + _eventAggregator.PublishEvent(new ApplicationShutdownRequested(true)); + if (_runtimeInfo.IsWindowsService) { _serviceProvider.Restart(ServiceProvider.NZBDRONE_SERVICE_NAME); } - _eventAggregator.PublishEvent(new ApplicationRestartRequested()); + else + { + //TODO: move this to environment specific projects + if (OsInfo.IsWindows) + { + if (_runtimeInfo.IsConsole) + { + //Run console with switch + var path = Path.Combine(_appFolderInfo.StartUpFolder, + ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME + ".exe"); + + _processProvider.SpawnNewProcess(path, "--terminateexisting --nobrowser"); + } + + else + { + var path = Path.Combine(_appFolderInfo.StartUpFolder, + ProcessProvider.NZB_DRONE_PROCESS_NAME + ".exe"); + + _processProvider.Start(path, "--terminateexisting --nobrowser"); + } + } + } } } } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 800258bde..83d567d8a 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -292,7 +292,6 @@ - diff --git a/src/NzbDrone.Host/ApplicationServer.cs b/src/NzbDrone.Host/ApplicationServer.cs index b506e64fd..4d1af5f06 100644 --- a/src/NzbDrone.Host/ApplicationServer.cs +++ b/src/NzbDrone.Host/ApplicationServer.cs @@ -75,7 +75,7 @@ namespace NzbDrone.Host 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 87448075b..2890d4ec3 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -36,7 +36,7 @@ namespace NzbDrone.Host var appMode = GetApplicationMode(startupContext); - Start(appMode); + Start(appMode, startupContext); if (startCallback != null) { @@ -54,11 +54,11 @@ namespace NzbDrone.Host } } - 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 @@ namespace NzbDrone.Host } } - private static void EnsureSingleInstance(bool isService) + private static void EnsureSingleInstance(bool isService, StartupContext startupContext) { var instancePolicy = _container.Resolve(); @@ -88,6 +88,10 @@ namespace NzbDrone.Host { instancePolicy.KillAllOtherInstance(); } + else if (startupContext.Flags.Contains(StartupContext.TERMINATE)) + { + instancePolicy.KillAllOtherInstance(); + } else { instancePolicy.PreventStartIfAlreadyRunning();