From ddff598ba4b209600573315978ef71eaf5e2ea93 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 13 Aug 2013 21:02:56 -0700 Subject: [PATCH] better routing. --- NzbDrone.App.Test/RouterTest.cs | 12 ++++-------- NzbDrone.Common/ConsoleService.cs | 4 ++-- NzbDrone.Common/ServiceProvider.cs | 9 ++++++++- NzbDrone.Host/Router.cs | 16 ++++++++-------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/NzbDrone.App.Test/RouterTest.cs b/NzbDrone.App.Test/RouterTest.cs index 9232f7784..fb046da66 100644 --- a/NzbDrone.App.Test/RouterTest.cs +++ b/NzbDrone.App.Test/RouterTest.cs @@ -58,11 +58,8 @@ public void Route_should_call_console_service_when_application_mode_is_console() Mocker.GetMock().Verify(c => c.Start(), Times.Once()); } - [TestCase(ApplicationModes.Interactive)] - [TestCase(ApplicationModes.InstallService)] - [TestCase(ApplicationModes.UninstallService)] - [TestCase(ApplicationModes.Help)] - public void Route_should_call_service_start_when_run_in_service_mode(ApplicationModes applicationModes) + [Test] + public void Route_should_call_service_start_when_run_in_service_mode() { var envMock = Mocker.GetMock(); var serviceProvider = Mocker.GetMock(); @@ -71,8 +68,9 @@ public void Route_should_call_service_start_when_run_in_service_mode(Application serviceProvider.Setup(c => c.Run(It.IsAny())); serviceProvider.Setup(c => c.ServiceExist(It.IsAny())).Returns(true); + serviceProvider.Setup(c => c.GetStatus(It.IsAny())).Returns(ServiceControllerStatus.StartPending); - Subject.Route(applicationModes); + Subject.Route(); serviceProvider.Verify(c => c.Run(It.IsAny()), Times.Once()); } @@ -90,7 +88,6 @@ public void show_error_on_install_if_service_already_exist() Subject.Route(ApplicationModes.InstallService); - Mocker.VerifyAllMocks(); } [Test] @@ -105,7 +102,6 @@ public void show_error_on_uninstall_if_service_doesnt_exist() Subject.Route(ApplicationModes.UninstallService); - Mocker.VerifyAllMocks(); } } } diff --git a/NzbDrone.Common/ConsoleService.cs b/NzbDrone.Common/ConsoleService.cs index c549dce01..4621624a7 100644 --- a/NzbDrone.Common/ConsoleService.cs +++ b/NzbDrone.Common/ConsoleService.cs @@ -34,8 +34,8 @@ public void PrintHelp() Console.WriteLine(); Console.WriteLine(" Usage: {0} ", Process.GetCurrentProcess().MainModule.ModuleName); Console.WriteLine(" Commands:"); - Console.WriteLine(" /{0} Install the application as a Windows Service ({1}).",StartupArguments.INSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME); - Console.WriteLine(" /{0} Uninstall already installed Windows Service ({1}).",StartupArguments.UNINSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME); + Console.WriteLine(" /{0} Install the application as a Windows Service ({1}).",StartupArguments.INSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME); + Console.WriteLine(" /{0} Uninstall already installed Windows Service ({1}).",StartupArguments.UNINSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME); Console.WriteLine(" /{0} Don't open NzbDrone in a browser", StartupArguments.NO_BROWSER); Console.WriteLine(" Run application in console mode."); } diff --git a/NzbDrone.Common/ServiceProvider.cs b/NzbDrone.Common/ServiceProvider.cs index a07072af9..7287e36df 100644 --- a/NzbDrone.Common/ServiceProvider.cs +++ b/NzbDrone.Common/ServiceProvider.cs @@ -18,6 +18,7 @@ public interface IServiceProvider ServiceController GetService(string serviceName); void Stop(string serviceName); void Start(string serviceName); + ServiceControllerStatus GetStatus(string serviceName); } public class ServiceProvider : IServiceProvider @@ -136,7 +137,13 @@ public virtual void Stop(string serviceName) } } - public virtual void Start(string serviceName) + + public ServiceControllerStatus GetStatus(string serviceName) + { + return GetService(serviceName).Status; + } + + public 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 db91a2b4a..869307553 100644 --- a/NzbDrone.Host/Router.cs +++ b/NzbDrone.Host/Router.cs @@ -93,6 +93,14 @@ public void Route(ApplicationModes applicationModes) private ApplicationModes GetApplicationMode() { + if (!_runtimeInfo.IsUserInteractive && + OsInfo.IsWindows && + _serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) && + _serviceProvider.GetStatus(ServiceProvider.NZBDRONE_SERVICE_NAME) == ServiceControllerStatus.StartPending) + { + return ApplicationModes.Service; + } + if (_startupArguments.Flags.Contains(StartupArguments.HELP)) { return ApplicationModes.Help; @@ -108,14 +116,6 @@ 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; } }