From 508be34ebc78de991e10f8460906b0a4d0bae615 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 20 Nov 2011 18:59:42 -0800 Subject: [PATCH] Update fixes. --- .gitignore | 3 +- .../NzbDrone.Common.Test.csproj | 1 + .../AppUpdateJobFixture.cs} | 29 ++++++---- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 2 +- NzbDrone.Core/Providers/Jobs/AppUpdateJob.cs | 58 ++++++++++++++++++- NzbDrone.Core/Providers/UpdateProvider.cs | 34 ----------- NzbDrone.Update/NzbDrone.Update.csproj | 1 + NzbDrone.Update/Program.cs | 23 +++----- config.xml | 6 -- 9 files changed, 87 insertions(+), 70 deletions(-) rename NzbDrone.Core.Test/{ProviderTests/UpdateProviderTests/StartUpdateFixture.cs => JobTests/AppUpdateJobFixture.cs} (86%) delete mode 100644 config.xml diff --git a/.gitignore b/.gitignore index 31ea5068e..b57d38499 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ NzbDrone.Web/NzbDrone.Web.Publish.xml _rawPackage/ NzbDrone.zip NzbDrone.sln.DotSettings.user* -config.xml \ No newline at end of file +config.xml +UpdateLogs/ \ No newline at end of file diff --git a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj index ffdf61160..bf433394f 100644 --- a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj +++ b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj @@ -40,6 +40,7 @@ ..\packages\Moq.4.0.10827\lib\NET40\Moq.dll + ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll diff --git a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/StartUpdateFixture.cs b/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs similarity index 86% rename from NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/StartUpdateFixture.cs rename to NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs index 13e3b7d33..d938605fc 100644 --- a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/StartUpdateFixture.cs +++ b/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs @@ -1,20 +1,21 @@ -using System; +using System.Linq; +using System; using System.Diagnostics; using System.IO; using FluentAssertions; using Moq; using NUnit.Framework; -using Ninject.Activation.Strategies; using NzbDrone.Common; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; +using NzbDrone.Core.Providers.Jobs; using NzbDrone.Core.Test.Framework; -namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests +namespace NzbDrone.Core.Test.JobTests { [TestFixture] - internal class StartUpdateFixture : CoreTest + internal class AppUpdateJobFixture : CoreTest { private const string SANDBOX_FOLDER = @"C:\Temp\nzbdrone_update\"; @@ -32,6 +33,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests { Mocker.GetMock().SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); Mocker.GetMock().SetupGet(c => c.Guid).Returns(_clientGuid); + Mocker.GetMock().Setup(c => c.GetAvilableUpdate()).Returns(updatePackage); } @@ -41,7 +43,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests Mocker.GetMock().Setup(c => c.FolderExists(SANDBOX_FOLDER)).Returns(true); //Act - Mocker.Resolve().StartUpdate(updatePackage); + StartUpdate(); //Assert Mocker.GetMock().Verify(c => c.DeleteFolder(SANDBOX_FOLDER, true)); @@ -53,7 +55,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests Mocker.GetMock().Setup(c => c.FolderExists(SANDBOX_FOLDER)).Returns(false); //Act - Mocker.Resolve().StartUpdate(updatePackage); + StartUpdate(); //Assert Mocker.GetMock().Verify(c => c.DeleteFolder(SANDBOX_FOLDER, true), Times.Never()); @@ -65,7 +67,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests var updateArchive = Path.Combine(SANDBOX_FOLDER, updatePackage.FileName); //Act - Mocker.Resolve().StartUpdate(updatePackage); + StartUpdate(); //Assert Mocker.GetMock().Verify( @@ -78,7 +80,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests var updateArchive = Path.Combine(SANDBOX_FOLDER, updatePackage.FileName); //Act - Mocker.Resolve().StartUpdate(updatePackage); + StartUpdate(); //Assert Mocker.GetMock().Verify( @@ -91,7 +93,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests var updateClientFolder = Mocker.GetMock().Object.GetUpdateClientFolder(); //Act - Mocker.Resolve().StartUpdate(updatePackage); + StartUpdate(); //Assert Mocker.GetMock().Verify( @@ -108,7 +110,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests .SetupGet(c => c.NzbDroneProcessIdFromEnviroment).Returns(12); //Act - Mocker.Resolve().StartUpdate(updatePackage); + StartUpdate(); //Assert Mocker.GetMock().Verify( @@ -134,7 +136,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests Mocker.Resolve(); Mocker.Resolve(); Mocker.Resolve(); - Mocker.Resolve().StartUpdate(updatePackage); + StartUpdate(); updateSubFolder.Refresh(); //Assert @@ -143,5 +145,10 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests updateSubFolder.GetDirectories().Should().HaveCount(1); updateSubFolder.GetFiles().Should().HaveCount(1); } + + private void StartUpdate() + { + Mocker.Resolve().Start(MockNotification, 0, 0); + } } } diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index ffefc8161..734ca6950 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -95,7 +95,7 @@ - + diff --git a/NzbDrone.Core/Providers/Jobs/AppUpdateJob.cs b/NzbDrone.Core/Providers/Jobs/AppUpdateJob.cs index f404f7bef..8dff9d465 100644 --- a/NzbDrone.Core/Providers/Jobs/AppUpdateJob.cs +++ b/NzbDrone.Core/Providers/Jobs/AppUpdateJob.cs @@ -1,14 +1,35 @@ -using NzbDrone.Core.Model.Notification; +using System.Diagnostics; +using System.IO; +using NLog; +using NzbDrone.Common; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers.Core; namespace NzbDrone.Core.Providers.Jobs { public class AppUpdateJob : IJob { private readonly UpdateProvider _updateProvider; + private readonly EnviromentProvider _enviromentProvider; + private readonly DiskProvider _diskProvider; + private readonly HttpProvider _httpProvider; + private readonly ProcessProvider _processProvider; + private readonly ArchiveProvider _archiveProvider; + private readonly ConfigFileProvider _configFileProvider; - public AppUpdateJob(UpdateProvider updateProvider) + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + + + public AppUpdateJob(UpdateProvider updateProvider, EnviromentProvider enviromentProvider, DiskProvider diskProvider, + HttpProvider httpProvider, ProcessProvider processProvider, ArchiveProvider archiveProvider, ConfigFileProvider configFileProvider) { _updateProvider = updateProvider; + _enviromentProvider = enviromentProvider; + _diskProvider = diskProvider; + _httpProvider = httpProvider; + _processProvider = processProvider; + _archiveProvider = archiveProvider; + _configFileProvider = configFileProvider; } public string Name @@ -27,7 +48,38 @@ namespace NzbDrone.Core.Providers.Jobs var updatePackage = _updateProvider.GetAvilableUpdate(); - _updateProvider.StartUpdate(updatePackage); + var packageDestination = Path.Combine(_enviromentProvider.GetUpdateSandboxFolder(), updatePackage.FileName); + + if (_diskProvider.FolderExists(_enviromentProvider.GetUpdateSandboxFolder())) + { + logger.Info("Deleting old update files"); + _diskProvider.DeleteFolder(_enviromentProvider.GetUpdateSandboxFolder(), true); + } + + logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination); + notification.CurrentMessage = "Downloading Update " + updatePackage.Version; + _httpProvider.DownloadFile(updatePackage.Url, packageDestination); + logger.Info("Download completed for update package from [{0}]", updatePackage.FileName); + + logger.Info("Extracting Update package"); + notification.CurrentMessage = "Extracting Update"; + _archiveProvider.ExtractArchive(packageDestination, _enviromentProvider.GetUpdateSandboxFolder()); + logger.Info("Update package extracted successfully"); + + logger.Info("Preparing client"); + notification.CurrentMessage = "Preparing to start Update"; + _diskProvider.CopyDirectory(_enviromentProvider.GetUpdateClientFolder(), _enviromentProvider.GetUpdateSandboxFolder()); + + + logger.Info("Starting update client"); + var startInfo = new ProcessStartInfo() + { + FileName = _enviromentProvider.GetUpdateClientExePath(), + Arguments = string.Format("{0} {1}", _enviromentProvider.NzbDroneProcessIdFromEnviroment, _configFileProvider.Guid) + }; + + var updateProcess = _processProvider.Start(startInfo); + logger.Info("Update in progress. NzbDrone will restart shortly."); } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/UpdateProvider.cs b/NzbDrone.Core/Providers/UpdateProvider.cs index c675bd606..040a689b6 100644 --- a/NzbDrone.Core/Providers/UpdateProvider.cs +++ b/NzbDrone.Core/Providers/UpdateProvider.cs @@ -77,39 +77,5 @@ namespace NzbDrone.Core.Providers logger.Trace("No updates available"); return null; } - - public virtual void StartUpdate(UpdatePackage updatePackage) - { - var packageDestination = Path.Combine(_enviromentProvider.GetUpdateSandboxFolder(), updatePackage.FileName); - - if (_diskProvider.FolderExists(_enviromentProvider.GetUpdateSandboxFolder())) - { - logger.Info("Deleting old update files"); - _diskProvider.DeleteFolder(_enviromentProvider.GetUpdateSandboxFolder(), true); - } - - logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination); - _httpProvider.DownloadFile(updatePackage.Url, packageDestination); - logger.Info("Download completed for update package from [{0}]", updatePackage.FileName); - - logger.Info("Extracting Update package"); - _archiveProvider.ExtractArchive(packageDestination, _enviromentProvider.GetUpdateSandboxFolder()); - logger.Info("Update package extracted successfully"); - - logger.Info("Preparing client"); - _diskProvider.CopyDirectory(_enviromentProvider.GetUpdateClientFolder(), _enviromentProvider.GetUpdateSandboxFolder()); - - - logger.Info("Starting update client"); - var startInfo = new ProcessStartInfo() - { - FileName = _enviromentProvider.GetUpdateClientExePath(), - Arguments = string.Format("{0} {1}", _enviromentProvider.NzbDroneProcessIdFromEnviroment, _configFileProvider.Guid) - }; - - _processProvider.Start(startInfo); - - } - } } diff --git a/NzbDrone.Update/NzbDrone.Update.csproj b/NzbDrone.Update/NzbDrone.Update.csproj index 1212ac035..772718f70 100644 --- a/NzbDrone.Update/NzbDrone.Update.csproj +++ b/NzbDrone.Update/NzbDrone.Update.csproj @@ -35,6 +35,7 @@ 4 + ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll diff --git a/NzbDrone.Update/Program.cs b/NzbDrone.Update/Program.cs index 2702780b3..8792aa85d 100644 --- a/NzbDrone.Update/Program.cs +++ b/NzbDrone.Update/Program.cs @@ -4,6 +4,7 @@ using System.Linq; using NLog; using NLog.Config; using NLog.Targets; +using Ninject; using NzbDrone.Common; using NzbDrone.Update.Providers; @@ -13,6 +14,7 @@ namespace NzbDrone.Update { private readonly UpdateProvider _updateProvider; private readonly ProcessProvider _processProvider; + private static StandardKernel _kernel; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); @@ -29,18 +31,10 @@ namespace NzbDrone.Update Console.WriteLine("Starting NzbDrone Update Client"); InitLoggers(); - logger.Info("Initializing update application"); - var enviromentProvider = new EnviromentProvider(); - var processProvider = new ProcessProvider(); - var serviceProvider = new ServiceProvider(); - var diskProvider = new DiskProvider(); - var iisProvider = new IISProvider(); - - var updateProvider = new UpdateProvider(diskProvider, serviceProvider, processProvider, enviromentProvider, iisProvider); - - new Program(updateProvider, processProvider).Start(args); + _kernel = new StandardKernel(); + _kernel.Get().Start(args); } catch (Exception e) { @@ -55,8 +49,8 @@ namespace NzbDrone.Update { try { - var enviromentProvider = new EnviromentProvider(); - var diskProvider = new DiskProvider(); + var enviromentProvider = _kernel.Get(); + var diskProvider = _kernel.Get(); logger.Info("Copying log tiles to application directory."); diskProvider.CopyDirectory(enviromentProvider.GetSandboxLogFolder(), enviromentProvider.GetUpdateLogFolder()); } @@ -70,18 +64,19 @@ namespace NzbDrone.Update { LogConfiguration.RegisterConsoleLogger(LogLevel.Trace); LogConfiguration.RegisterUdpLogger(); - LogConfiguration.RegisterExceptioneer(); + var lastUpgradeLog = new FileTarget(); lastUpgradeLog.AutoFlush = true; lastUpgradeLog.ConcurrentWrites = false; lastUpgradeLog.FileName = Path.Combine(PathExtentions.UPDATE_LOG_FOLDER_NAME, DateTime.Now.ToString("yyyy.MM.dd-H-mm") + ".txt"); lastUpgradeLog.KeepFileOpen = false; - lastUpgradeLog.Layout = "${longdate} - ${logger}: ${message} ${exception}"; + lastUpgradeLog.Layout = "${longdate} - ${logger}: ${message} ${exception:format=ToString}"; LogManager.Configuration.AddTarget(lastUpgradeLog.GetType().Name, lastUpgradeLog); LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, lastUpgradeLog)); + LogConfiguration.RegisterExceptioneer(); LogConfiguration.Reload(); } diff --git a/config.xml b/config.xml deleted file mode 100644 index 6a1a578ab..000000000 --- a/config.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - 8989 - 0 - true - \ No newline at end of file