From 3b4eacdd3138a93f7780182e41b18ff6b1287424 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 13 Aug 2013 20:48:50 -0700 Subject: [PATCH] better service routing --- NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs | 1 + NzbDrone.Common/ServiceProvider.cs | 17 +++++++++-------- NzbDrone.Host/Router.cs | 16 ++++++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) 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 @@ namespace NzbDrone.Common 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 @@ namespace NzbDrone.Common 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 @@ namespace NzbDrone.Common 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 @@ namespace NzbDrone.Common 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 @@ namespace NzbDrone.Common 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 @@ namespace NzbDrone.Common 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 @@ namespace NzbDrone.Common } } - 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 @@ namespace NzbDrone.Host 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 @@ namespace NzbDrone.Host 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; } }