2012-01-15 05:47:23 +03:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2011-12-02 04:33:17 +03:00
|
|
|
using System.Diagnostics;
|
2011-11-21 05:59:42 +03:00
|
|
|
using System.IO;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-12-02 04:33:17 +03:00
|
|
|
using NzbDrone.Core.Providers;
|
2011-11-21 05:59:42 +03:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-11-14 05:54:09 +03:00
|
|
|
|
2011-12-02 04:33:17 +03:00
|
|
|
namespace NzbDrone.Core.Jobs
|
2011-11-14 05:54:09 +03:00
|
|
|
{
|
|
|
|
public class AppUpdateJob : IJob
|
|
|
|
{
|
|
|
|
private readonly UpdateProvider _updateProvider;
|
2011-11-21 05:59:42 +03:00
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
private readonly HttpProvider _httpProvider;
|
|
|
|
private readonly ProcessProvider _processProvider;
|
|
|
|
private readonly ArchiveProvider _archiveProvider;
|
|
|
|
private readonly ConfigFileProvider _configFileProvider;
|
2011-11-14 05:54:09 +03:00
|
|
|
|
2011-11-21 05:59:42 +03:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
|
|
public AppUpdateJob(UpdateProvider updateProvider, EnviromentProvider enviromentProvider, DiskProvider diskProvider,
|
|
|
|
HttpProvider httpProvider, ProcessProvider processProvider, ArchiveProvider archiveProvider, ConfigFileProvider configFileProvider)
|
2011-11-14 05:54:09 +03:00
|
|
|
{
|
|
|
|
_updateProvider = updateProvider;
|
2011-11-21 05:59:42 +03:00
|
|
|
_enviromentProvider = enviromentProvider;
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
_processProvider = processProvider;
|
|
|
|
_archiveProvider = archiveProvider;
|
|
|
|
_configFileProvider = configFileProvider;
|
2011-11-14 05:54:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return "Update Application Job"; }
|
|
|
|
}
|
|
|
|
|
2012-01-15 05:47:23 +03:00
|
|
|
public TimeSpan DefaultInterval
|
2011-11-14 05:54:09 +03:00
|
|
|
{
|
2012-01-15 05:47:23 +03:00
|
|
|
get { return TimeSpan.FromDays(2); }
|
2011-11-14 05:54:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
|
|
|
|
{
|
|
|
|
notification.CurrentMessage = "Updating NzbDrone";
|
|
|
|
|
|
|
|
var updatePackage = _updateProvider.GetAvilableUpdate();
|
|
|
|
|
2011-11-21 05:59:42 +03:00
|
|
|
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";
|
2011-11-22 07:42:05 +03:00
|
|
|
_diskProvider.MoveDirectory(_enviromentProvider.GetUpdateClientFolder(), _enviromentProvider.GetUpdateSandboxFolder());
|
2011-11-21 05:59:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
2011-11-21 09:21:36 +03:00
|
|
|
notification.CurrentMessage = "Update in progress. NzbDrone will restart shortly.";
|
2011-11-14 05:54:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|