2012-01-15 03:47:23 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2011-12-02 02:33:17 +01:00
|
|
|
|
using System.Diagnostics;
|
2011-11-21 03:59:42 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-02-18 08:59:43 +01:00
|
|
|
|
using NzbDrone.Core.Jobs;
|
2011-11-21 03:59:42 +01:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-12-02 02:33:17 +01:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2011-11-14 03:54:09 +01:00
|
|
|
|
|
2013-02-18 08:59:43 +01:00
|
|
|
|
namespace NzbDrone.Core.Lifecycle
|
2011-11-14 03:54:09 +01:00
|
|
|
|
{
|
|
|
|
|
public class AppUpdateJob : IJob
|
|
|
|
|
{
|
|
|
|
|
private readonly UpdateProvider _updateProvider;
|
2012-03-07 03:59:43 +01:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2011-11-21 03:59:42 +01:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2013-04-11 01:41:45 +02:00
|
|
|
|
private readonly IHttpProvider _httpProvider;
|
2011-11-21 03:59:42 +01:00
|
|
|
|
private readonly ProcessProvider _processProvider;
|
|
|
|
|
private readonly ArchiveProvider _archiveProvider;
|
|
|
|
|
private readonly ConfigFileProvider _configFileProvider;
|
2011-11-14 03:54:09 +01:00
|
|
|
|
|
2011-11-21 03:59:42 +01:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2012-03-07 03:59:43 +01:00
|
|
|
|
public AppUpdateJob(UpdateProvider updateProvider, EnvironmentProvider environmentProvider, DiskProvider diskProvider,
|
2013-04-11 07:08:55 +02:00
|
|
|
|
IHttpProvider httpProvider, ProcessProvider processProvider, ArchiveProvider archiveProvider, ConfigFileProvider configFileProvider)
|
2011-11-14 03:54:09 +01:00
|
|
|
|
{
|
|
|
|
|
_updateProvider = updateProvider;
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_environmentProvider = environmentProvider;
|
2011-11-21 03:59:42 +01:00
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
|
_processProvider = processProvider;
|
|
|
|
|
_archiveProvider = archiveProvider;
|
|
|
|
|
_configFileProvider = configFileProvider;
|
2011-11-14 03:54:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Update Application Job"; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-15 03:47:23 +01:00
|
|
|
|
public TimeSpan DefaultInterval
|
2011-11-14 03:54:09 +01:00
|
|
|
|
{
|
2012-01-15 03:47:23 +01:00
|
|
|
|
get { return TimeSpan.FromDays(2); }
|
2011-11-14 03:54:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 21:04:17 +02:00
|
|
|
|
public virtual void Start(ProgressNotification notification, dynamic options)
|
2012-02-26 22:22:35 +01:00
|
|
|
|
{
|
2012-01-27 06:26:39 +01:00
|
|
|
|
notification.CurrentMessage = "Checking for updates";
|
2011-11-14 03:54:09 +01:00
|
|
|
|
|
2012-03-07 03:59:43 +01:00
|
|
|
|
var updatePackage = _updateProvider.GetAvilableUpdate(_environmentProvider.Version);
|
2011-11-14 03:54:09 +01:00
|
|
|
|
|
2012-01-16 19:10:18 +01:00
|
|
|
|
//No updates available
|
|
|
|
|
if (updatePackage == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-03-07 03:59:43 +01:00
|
|
|
|
var packageDestination = Path.Combine(_environmentProvider.GetUpdateSandboxFolder(), updatePackage.FileName);
|
2011-11-21 03:59:42 +01:00
|
|
|
|
|
2012-03-07 03:59:43 +01:00
|
|
|
|
if (_diskProvider.FolderExists(_environmentProvider.GetUpdateSandboxFolder()))
|
2011-11-21 03:59:42 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Deleting old update files");
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_diskProvider.DeleteFolder(_environmentProvider.GetUpdateSandboxFolder(), true);
|
2011-11-21 03:59:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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";
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_archiveProvider.ExtractArchive(packageDestination, _environmentProvider.GetUpdateSandboxFolder());
|
2011-11-21 03:59:42 +01:00
|
|
|
|
logger.Info("Update package extracted successfully");
|
|
|
|
|
|
|
|
|
|
logger.Info("Preparing client");
|
|
|
|
|
notification.CurrentMessage = "Preparing to start Update";
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_diskProvider.MoveDirectory(_environmentProvider.GetUpdateClientFolder(), _environmentProvider.GetUpdateSandboxFolder());
|
2011-11-21 03:59:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.Info("Starting update client");
|
2012-01-16 19:10:18 +01:00
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
|
|
|
{
|
2012-03-07 03:59:43 +01:00
|
|
|
|
FileName = _environmentProvider.GetUpdateClientExePath(),
|
2013-03-30 22:29:02 +01:00
|
|
|
|
Arguments = string.Format("{0} {1}", _environmentProvider.NzbDroneProcessIdFromEnvironment, _configFileProvider.Guid)
|
2012-02-26 22:22:35 +01:00
|
|
|
|
};
|
2011-11-21 03:59:42 +01:00
|
|
|
|
|
2012-02-26 22:22:35 +01:00
|
|
|
|
var process = _processProvider.Start(startInfo);
|
2011-11-21 07:21:36 +01:00
|
|
|
|
notification.CurrentMessage = "Update in progress. NzbDrone will restart shortly.";
|
2012-02-26 22:22:35 +01:00
|
|
|
|
|
|
|
|
|
_processProvider.WaitForExit(process);
|
2011-11-14 03:54:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|