From d0d95602c6960d8a2922176321158d7d7eda15bb Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 14 Jun 2013 08:21:12 -0700 Subject: [PATCH] When restarting after update if service start fails update app will fallback to console. --- ...ture.cs => InstallUpdateServiceFixture.cs} | 10 ++--- .../NzbDrone.Update.Test.csproj | 3 +- NzbDrone.Update.Test/StartNzbDroneService.cs | 40 +++++++++++++++++++ NzbDrone.Update/UpdateEngine/StartNzbDrone.cs | 38 +++++++++++++++--- 4 files changed, 78 insertions(+), 13 deletions(-) rename NzbDrone.Update.Test/{UpdateProviderVerifyFixture.cs => InstallUpdateServiceFixture.cs} (78%) create mode 100644 NzbDrone.Update.Test/StartNzbDroneService.cs diff --git a/NzbDrone.Update.Test/UpdateProviderVerifyFixture.cs b/NzbDrone.Update.Test/InstallUpdateServiceFixture.cs similarity index 78% rename from NzbDrone.Update.Test/UpdateProviderVerifyFixture.cs rename to NzbDrone.Update.Test/InstallUpdateServiceFixture.cs index 8c8b5b392..1c73285dd 100644 --- a/NzbDrone.Update.Test/UpdateProviderVerifyFixture.cs +++ b/NzbDrone.Update.Test/InstallUpdateServiceFixture.cs @@ -9,10 +9,8 @@ using NzbDrone.Update.UpdateEngine; namespace NzbDrone.Update.Test { [TestFixture] - public class UpdateProviderVerifyFixture : TestBase + public class InstallUpdateServiceFixture : TestBase { - - [SetUp] public void Setup() { @@ -25,7 +23,7 @@ namespace NzbDrone.Update.Test [TestCase(" ")] public void update_should_throw_target_folder_is_blank(string target) { - Assert.Throws(() => Mocker.Resolve().Start(target)) + Assert.Throws(() => Subject.Start(target)) .Message.Should().StartWith("Target folder can not be null or empty"); } @@ -34,7 +32,7 @@ namespace NzbDrone.Update.Test { string targetFolder = "c:\\NzbDrone\\"; - Assert.Throws(() => Mocker.Resolve().Start(targetFolder)) + Assert.Throws(() => Subject.Start(targetFolder)) .Message.Should().StartWith("Target folder doesn't exist"); } @@ -52,7 +50,7 @@ namespace NzbDrone.Update.Test .Setup(c => c.FolderExists(sandboxFolder)) .Returns(false); - Assert.Throws(() => Mocker.Resolve().Start(targetFolder)) + Assert.Throws(() => Subject.Start(targetFolder)) .Message.Should().StartWith("Update folder doesn't exist"); } } diff --git a/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj b/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj index 78193ac90..eee955f2d 100644 --- a/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj +++ b/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj @@ -85,8 +85,9 @@ + - + diff --git a/NzbDrone.Update.Test/StartNzbDroneService.cs b/NzbDrone.Update.Test/StartNzbDroneService.cs new file mode 100644 index 000000000..370f56d10 --- /dev/null +++ b/NzbDrone.Update.Test/StartNzbDroneService.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common; +using NzbDrone.Test.Common; +using NzbDrone.Update.UpdateEngine; + +namespace NzbDrone.Update.Test +{ + [TestFixture] + public class StartNzbDroneServiceFixture : TestBase + { + [Test] + public void should_start_service_if_app_type_was_serivce() + { + 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() + { + string targetFolder = "c:\\NzbDrone\\"; + + Mocker.GetMock().Setup(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME)).Throws(new InvalidOperationException()); + + Subject.Start(AppType.Service, targetFolder); + + Mocker.GetMock().Verify(c => c.Start("c:\\NzbDrone\\NzbDrone.Console.exe"), Times.Once()); + + ExceptionVerification.ExpectedWarns(1); + } + } +} diff --git a/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs b/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs index b20d26437..93f9ae0b7 100644 --- a/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs +++ b/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs @@ -1,6 +1,8 @@ +using System; using System.IO; using NLog; using NzbDrone.Common; +using IServiceProvider = NzbDrone.Common.IServiceProvider; namespace NzbDrone.Update.UpdateEngine { @@ -27,19 +29,43 @@ namespace NzbDrone.Update.UpdateEngine _logger.Info("Starting NzbDrone"); if (appType == AppType.Service) { - _logger.Info("Starting NzbDrone service"); - _serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME); + try + { + StartService(); + + } + catch (InvalidOperationException e) + { + _logger.Warn("Couldn't start NzbDrone Service (Most likely due to permission issues). falling back to console.", e); + StartConsole(installationFolder); + } } else if (appType == AppType.Console) { - _logger.Info("Starting NzbDrone with Console"); - _processProvider.Start(Path.Combine(installationFolder, "NzbDrone.Console.exe")); + StartConsole(installationFolder); } else { - _logger.Info("Starting NzbDrone without Console"); - _processProvider.Start(Path.Combine(installationFolder, "NzbDrone.exe")); + StartWinform(installationFolder); } } + + private void StartService() + { + _logger.Info("Starting NzbDrone service"); + _serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME); + } + + private void StartWinform(string installationFolder) + { + _logger.Info("Starting NzbDrone without Console"); + _processProvider.Start(Path.Combine(installationFolder, "NzbDrone.exe")); + } + + private void StartConsole(string installationFolder) + { + _logger.Info("Starting NzbDrone with Console"); + _processProvider.Start(Path.Combine(installationFolder, "NzbDrone.Console.exe")); + } } } \ No newline at end of file