From 75a46a3abebdc1997433a05667852a12308134c4 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Tue, 20 Aug 2013 15:12:35 -0700 Subject: [PATCH] fixed NzbDrone using 100% cpu when console not available. --- NzbDrone.App.Test/RouterTest.cs | 2 -- NzbDrone.Common/ConsoleService.cs | 16 +++---------- NzbDrone.Common/ProcessProvider.cs | 24 +++++++++++++++++++ NzbDrone.Console/ConsoleApp.cs | 11 ++++++--- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 - NzbDrone.Core/NzbDrone.Core.csproj | 1 - NzbDrone.Host/Router.cs | 5 ---- NzbDrone.Update.Test/StartNzbDroneService.cs | 7 +++--- NzbDrone.Update/UpdateEngine/StartNzbDrone.cs | 3 +-- 9 files changed, 39 insertions(+), 31 deletions(-) diff --git a/NzbDrone.App.Test/RouterTest.cs b/NzbDrone.App.Test/RouterTest.cs index fb046da66..569af6a9f 100644 --- a/NzbDrone.App.Test/RouterTest.cs +++ b/NzbDrone.App.Test/RouterTest.cs @@ -50,11 +50,9 @@ namespace NzbDrone.App.Test public void Route_should_call_console_service_when_application_mode_is_console() { Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); - Mocker.GetMock().SetupGet(c => c.IsConsoleApplication).Returns(true); Subject.Route(ApplicationModes.Interactive); - Mocker.GetMock().Verify(c => c.WaitForClose(), Times.Once()); Mocker.GetMock().Verify(c => c.Start(), Times.Once()); } diff --git a/NzbDrone.Common/ConsoleService.cs b/NzbDrone.Common/ConsoleService.cs index 4621624a7..cceeade42 100644 --- a/NzbDrone.Common/ConsoleService.cs +++ b/NzbDrone.Common/ConsoleService.cs @@ -7,8 +7,6 @@ namespace NzbDrone.Common { public interface IConsoleService { - bool IsConsoleApplication { get; } - void WaitForClose(); void PrintHelp(); void PrintServiceAlreadyExist(); void PrintServiceDoesNotExist(); @@ -16,26 +14,18 @@ namespace NzbDrone.Common public class ConsoleService : IConsoleService { - public bool IsConsoleApplication + public static bool IsConsoleAvailable { get { return Console.In != StreamReader.Null; } } - public void WaitForClose() - { - while (true) - { - Console.ReadLine(); - } - } - 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/ProcessProvider.cs b/NzbDrone.Common/ProcessProvider.cs index 0c81ccf38..8df4198e8 100644 --- a/NzbDrone.Common/ProcessProvider.cs +++ b/NzbDrone.Common/ProcessProvider.cs @@ -21,6 +21,7 @@ namespace NzbDrone.Common bool Exists(string processName); ProcessPriorityClass GetCurrentProcessPriority(); Process Start(string path, string args = null, Action onOutputDataReceived = null, Action onErrorDataReceived = null); + Process SpawnNewProcess(string path, string args = null); } public class ProcessProvider : IProcessProvider @@ -86,6 +87,8 @@ namespace NzbDrone.Common process.Start(); } + + public Process Start(string path, string args = null, Action onOutputDataReceived = null, Action onErrorDataReceived = null) { @@ -147,6 +150,27 @@ namespace NzbDrone.Common return process; } + public Process SpawnNewProcess(string path, string args = null) + { + if (OsInfo.IsMono && path.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase)) + { + args = path + " " + args; + path = "mono"; + } + + Logger.Info("Starting {0} {1}", path, args); + + var startInfo = new ProcessStartInfo(path, args); + var process = new Process + { + StartInfo = startInfo + }; + + process.Start(); + + return process; + } + public void WaitForExit(Process process) { Logger.Trace("Waiting for process {0} to exit.", process.ProcessName); diff --git a/NzbDrone.Console/ConsoleApp.cs b/NzbDrone.Console/ConsoleApp.cs index 36bf92f65..f9d55a599 100644 --- a/NzbDrone.Console/ConsoleApp.cs +++ b/NzbDrone.Console/ConsoleApp.cs @@ -1,4 +1,5 @@ using System; +using NzbDrone.Common; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Host; @@ -12,15 +13,19 @@ namespace NzbDrone.Console { Bootstrap.Start(new StartupArguments(args), new ConsoleAlerts()); } - catch (TerminateApplicationException) + catch (TerminateApplicationException) { } catch (Exception e) { System.Console.WriteLine(e.ToString()); } - System.Console.WriteLine("Press enter to exit..."); - System.Console.ReadLine(); + + if (ConsoleService.IsConsoleAvailable) + { + System.Console.WriteLine("Press enter to exit..."); + System.Console.ReadLine(); + } } } } \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 0cfb64c94..a65e1eb59 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -133,7 +133,6 @@ - diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 67dfebd11..25b97bc3e 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -227,7 +227,6 @@ - diff --git a/NzbDrone.Host/Router.cs b/NzbDrone.Host/Router.cs index 869307553..76d9e1583 100644 --- a/NzbDrone.Host/Router.cs +++ b/NzbDrone.Host/Router.cs @@ -48,11 +48,6 @@ namespace NzbDrone.Host { _logger.Trace("Console selected"); _nzbDroneServiceFactory.Start(); - if (_consoleService.IsConsoleApplication) - { - _consoleService.WaitForClose(); - } - break; } case ApplicationModes.InstallService: diff --git a/NzbDrone.Update.Test/StartNzbDroneService.cs b/NzbDrone.Update.Test/StartNzbDroneService.cs index 7bd2b047f..4b1c0c4bb 100644 --- a/NzbDrone.Update.Test/StartNzbDroneService.cs +++ b/NzbDrone.Update.Test/StartNzbDroneService.cs @@ -15,16 +15,15 @@ namespace NzbDrone.Update.Test [Test] public void should_start_service_if_app_type_was_serivce() { - string targetFolder = "c:\\NzbDrone\\"; + const string targetFolder = "c:\\NzbDrone\\"; Subject.Start(AppType.Service, targetFolder); Mocker.GetMock().Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); } - [Test] - public void should_start_console_if_app_type_was_serivce_but_start_failed_because_of_permissions() + public void should_start_console_if_app_type_was_service_but_start_failed_because_of_permissions() { const string targetFolder = "c:\\NzbDrone\\"; @@ -32,7 +31,7 @@ namespace NzbDrone.Update.Test Subject.Start(AppType.Service, targetFolder); - Mocker.GetMock().Verify(c => c.Start("c:\\NzbDrone\\NzbDrone.Console.exe", StartupArguments.NO_BROWSER, null, null), Times.Once()); + Mocker.GetMock().Verify(c => c.SpawnNewProcess("c:\\NzbDrone\\NzbDrone.Console.exe", StartupArguments.NO_BROWSER), Times.Once()); ExceptionVerification.ExpectedWarns(1); } diff --git a/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs b/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs index 1a419bb7f..e7ad7753c 100644 --- a/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs +++ b/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.IO; using NLog; using NzbDrone.Common; @@ -73,7 +72,7 @@ namespace NzbDrone.Update.UpdateEngine _logger.Info("Starting {0}", fileName); var path = Path.Combine(installationFolder, fileName); - _processProvider.Start(path, StartupArguments.NO_BROWSER); + _processProvider.SpawnNewProcess(path, StartupArguments.NO_BROWSER); } } } \ No newline at end of file