Radarr/NzbDrone.Core/Update/InstallUpdateService.cs

89 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.IO;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Update.Commands;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.Instrumentation;
namespace NzbDrone.Core.Update
{
public class InstallUpdateService : IExecute<ApplicationUpdateCommand>
{
private readonly ICheckUpdateService _checkUpdateService;
private readonly Logger _logger;
private readonly IAppFolderInfo _appFolderInfo;
private readonly IDiskProvider _diskProvider;
private readonly IHttpProvider _httpProvider;
private readonly IArchiveService _archiveService;
private readonly IProcessProvider _processProvider;
public InstallUpdateService(ICheckUpdateService checkUpdateService, IAppFolderInfo appFolderInfo,
IDiskProvider diskProvider, IHttpProvider httpProvider,
IArchiveService archiveService, IProcessProvider processProvider, Logger logger)
{
_checkUpdateService = checkUpdateService;
_appFolderInfo = appFolderInfo;
_diskProvider = diskProvider;
_httpProvider = httpProvider;
_archiveService = archiveService;
_processProvider = processProvider;
_logger = logger;
}
public void Execute(ApplicationUpdateCommand message)
{
2013-09-11 06:33:47 +00:00
_logger.ProgressDebug("Checking for updates");
var latestAvailable = _checkUpdateService.AvailableUpdate();
if (latestAvailable != null)
{
InstallUpdate(latestAvailable);
}
}
private void InstallUpdate(UpdatePackage updatePackage)
{
try
{
var updateSandboxFolder = _appFolderInfo.GetUpdateSandboxFolder();
var packageDestination = Path.Combine(updateSandboxFolder, updatePackage.FileName);
if (_diskProvider.FolderExists(updateSandboxFolder))
{
_logger.Info("Deleting old update files");
_diskProvider.DeleteFolder(updateSandboxFolder, true);
}
2013-09-19 05:33:37 +00:00
_logger.ProgressInfo("Downloading update {0} [{1}]", updatePackage.Version, updatePackage.Branch);
2013-09-11 06:33:47 +00:00
_logger.Debug("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination);
_httpProvider.DownloadFile(updatePackage.Url, packageDestination);
2013-09-11 06:33:47 +00:00
_logger.ProgressInfo("Extracting Update package");
_archiveService.Extract(packageDestination, updateSandboxFolder);
_logger.Info("Update package extracted successfully");
_logger.Info("Preparing client");
_diskProvider.MoveFolder(_appFolderInfo.GetUpdateClientFolder(),
updateSandboxFolder);
2013-08-07 05:54:50 +00:00
_logger.Info("Starting update client {0}", _appFolderInfo.GetUpdateClientExePath());
2013-09-11 06:33:47 +00:00
_logger.ProgressInfo("NzbDrone will restart shortly.");
_processProvider.Start(_appFolderInfo.GetUpdateClientExePath(), _processProvider.GetCurrentProcess().Id.ToString());
}
catch (Exception ex)
{
_logger.ErrorException("Update process failed", ex);
}
}
}
2013-08-07 05:54:50 +00:00
}