You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/NzbDrone.Core/Lifecycle/AppUpdateJob.cs

93 lines
3.9 KiB

using System;
using System.Linq;
using System.Diagnostics;
13 years ago
using System.IO;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.Jobs;
13 years ago
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
13 years ago
namespace NzbDrone.Core.Lifecycle
13 years ago
{
public class AppUpdateJob : IJob
{
private readonly UpdateProvider _updateProvider;
private readonly EnvironmentProvider _environmentProvider;
13 years ago
private readonly DiskProvider _diskProvider;
private readonly HttpProvider _httpProvider;
private readonly ProcessProvider _processProvider;
private readonly ArchiveProvider _archiveProvider;
private readonly ConfigFileProvider _configFileProvider;
13 years ago
13 years ago
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public AppUpdateJob(UpdateProvider updateProvider, EnvironmentProvider environmentProvider, DiskProvider diskProvider,
13 years ago
HttpProvider httpProvider, ProcessProvider processProvider, ArchiveProvider archiveProvider, ConfigFileProvider configFileProvider)
13 years ago
{
_updateProvider = updateProvider;
_environmentProvider = environmentProvider;
13 years ago
_diskProvider = diskProvider;
_httpProvider = httpProvider;
_processProvider = processProvider;
_archiveProvider = archiveProvider;
_configFileProvider = configFileProvider;
13 years ago
}
public string Name
{
get { return "Update Application Job"; }
}
public TimeSpan DefaultInterval
13 years ago
{
get { return TimeSpan.FromDays(2); }
13 years ago
}
public virtual void Start(ProgressNotification notification, dynamic options)
{
notification.CurrentMessage = "Checking for updates";
13 years ago
var updatePackage = _updateProvider.GetAvilableUpdate(_environmentProvider.Version);
13 years ago
//No updates available
if (updatePackage == null)
return;
var packageDestination = Path.Combine(_environmentProvider.GetUpdateSandboxFolder(), updatePackage.FileName);
13 years ago
if (_diskProvider.FolderExists(_environmentProvider.GetUpdateSandboxFolder()))
13 years ago
{
logger.Info("Deleting old update files");
_diskProvider.DeleteFolder(_environmentProvider.GetUpdateSandboxFolder(), true);
13 years ago
}
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, _environmentProvider.GetUpdateSandboxFolder());
13 years ago
logger.Info("Update package extracted successfully");
logger.Info("Preparing client");
notification.CurrentMessage = "Preparing to start Update";
_diskProvider.MoveDirectory(_environmentProvider.GetUpdateClientFolder(), _environmentProvider.GetUpdateSandboxFolder());
13 years ago
logger.Info("Starting update client");
var startInfo = new ProcessStartInfo
{
FileName = _environmentProvider.GetUpdateClientExePath(),
Arguments = string.Format("{0} {1}", _environmentProvider.NzbDroneProcessIdFromEnvironment, _configFileProvider.Guid)
};
13 years ago
var process = _processProvider.Start(startInfo);
notification.CurrentMessage = "Update in progress. NzbDrone will restart shortly.";
_processProvider.WaitForExit(process);
13 years ago
}
}
}