diff --git a/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs b/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs index f2d0e4caf..7bd6e8d3c 100644 --- a/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs +++ b/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using System.Security.Principal; +using System.ServiceProcess; using NLog; namespace NzbDrone.Common.EnvironmentInfo diff --git a/NzbDrone.Common/ServiceProvider.cs b/NzbDrone.Common/ServiceProvider.cs index ea44f5d6c..a07072af9 100644 --- a/NzbDrone.Common/ServiceProvider.cs +++ b/NzbDrone.Common/ServiceProvider.cs @@ -15,6 +15,7 @@ public interface IServiceProvider void Install(string serviceName); void UnInstall(string serviceName); void Run(ServiceBase service); + ServiceController GetService(string serviceName); void Stop(string serviceName); void Start(string serviceName); } @@ -25,7 +26,7 @@ public class ServiceProvider : IServiceProvider private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public bool ServiceExist(string name) + public virtual bool ServiceExist(string name) { Logger.Debug("Checking if service {0} exists.", name); return @@ -33,7 +34,7 @@ public bool ServiceExist(string name) s => String.Equals(s.ServiceName, name, StringComparison.InvariantCultureIgnoreCase)); } - public bool IsServiceRunning(string name) + public virtual bool IsServiceRunning(string name) { Logger.Debug("Checking if '{0}' service is running", name); @@ -47,7 +48,7 @@ public bool IsServiceRunning(string name) service.Status == ServiceControllerStatus.PausePending); } - public void Install(string serviceName) + public virtual void Install(string serviceName) { Logger.Info("Installing service '{0}'", serviceName); @@ -76,7 +77,7 @@ public void Install(string serviceName) Logger.Info("Service Has installed successfully."); } - public void UnInstall(string serviceName) + public virtual void UnInstall(string serviceName) { Logger.Info("Uninstalling {0} service", serviceName); @@ -92,17 +93,17 @@ public void UnInstall(string serviceName) Logger.Info("{0} successfully uninstalled", serviceName); } - public void Run(ServiceBase service) + public virtual void Run(ServiceBase service) { ServiceBase.Run(service); } - public ServiceController GetService(string serviceName) + public virtual ServiceController GetService(string serviceName) { return ServiceController.GetServices().FirstOrDefault(c => String.Equals(c.ServiceName, serviceName, StringComparison.InvariantCultureIgnoreCase)); } - public void Stop(string serviceName) + public virtual void Stop(string serviceName) { Logger.Info("Stopping {0} Service...", serviceName); var service = GetService(serviceName); @@ -135,7 +136,7 @@ public void Stop(string serviceName) } } - public void Start(string serviceName) + public virtual void Start(string serviceName) { Logger.Info("Starting {0} Service...", serviceName); var service = GetService(serviceName); diff --git a/NzbDrone.Host/Router.cs b/NzbDrone.Host/Router.cs index eb27a8aa3..db91a2b4a 100644 --- a/NzbDrone.Host/Router.cs +++ b/NzbDrone.Host/Router.cs @@ -1,4 +1,5 @@ -using NLog; +using System.ServiceProcess; +using NLog; using NzbDrone.Common; using NzbDrone.Common.EnvironmentInfo; @@ -32,11 +33,6 @@ public void Route() public void Route(ApplicationModes applicationModes) { - if (!_runtimeInfo.IsUserInteractive && !OsInfo.IsLinux &&_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) - { - applicationModes = ApplicationModes.Service; - } - _logger.Info("Application mode: {0}", applicationModes); switch (applicationModes) @@ -112,6 +108,14 @@ private ApplicationModes GetApplicationMode() return ApplicationModes.UninstallService; } + if (!_runtimeInfo.IsUserInteractive && + OsInfo.IsWindows && + _serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) && + _serviceProvider.GetService(ServiceProvider.NZBDRONE_SERVICE_NAME).Status == ServiceControllerStatus.StartPending) + { + return ApplicationModes.Service; + } + return ApplicationModes.Interactive; } }