PhysicalFilesDownloader
using Relativity.DataExchange.Export.VolumeManagerV2.Download.TapiHelpers;
using Relativity.DataExchange.Export.VolumeManagerV2.Statistics;
using Relativity.DataExchange.Logger;
using Relativity.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Relativity.DataExchange.Export.VolumeManagerV2.Download
{
public class PhysicalFilesDownloader : IPhysicalFilesDownloader
{
private readonly IFileShareSettingsService _settingsService;
private readonly IFileTapiBridgePool _fileTapiBridgePool;
private readonly IDownloadProgressManager _downloadProgressManager;
private readonly ILog _logger;
private readonly SafeIncrement _safeIncrement;
public PhysicalFilesDownloader(IFileShareSettingsService settingsService, IFileTapiBridgePool fileTapiBridgePool, IDownloadProgressManager downloadProgressManager, SafeIncrement safeIncrement, ILog logger)
{
_settingsService = settingsService.ThrowIfNull("settingsService");
_fileTapiBridgePool = fileTapiBridgePool.ThrowIfNull("fileTapiBridgePool");
_downloadProgressManager = downloadProgressManager.ThrowIfNull("downloadProgressManager");
_safeIncrement = safeIncrement.ThrowIfNull("safeIncrement");
_logger = logger.ThrowIfNull<ILog>("logger");
}
[AsyncStateMachine(typeof(<DownloadFilesAsync>d__6))]
public Task DownloadFilesAsync(List<ExportRequest> requests, CancellationToken token)
{
<DownloadFilesAsync>d__6 stateMachine = default(<DownloadFilesAsync>d__6);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.requests = requests;
stateMachine.token = token;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
private List<ExportRequestsWithFileshareSettings> (List<ExportRequest> requests)
{
return new List<ExportRequestsWithFileshareSettings>(from r in requests.ToLookup((ExportRequest r) => _settingsService.GetSettingsForFileShare(r.ArtifactId, r.SourceLocation))
select new ExportRequestsWithFileshareSettings(r.Key, r));
}
private void (ExportRequestsWithFileshareSettings settings, CancellationToken token)
{
IDownloadTapiBridge downloadTapiBridge = _fileTapiBridgePool.Request(settings.FileshareSettings, token);
DownloadFiles(downloadTapiBridge, settings.Requests, token);
downloadTapiBridge.WaitForTransfers();
}
private void DownloadFiles(IDownloadTapiBridge bridge, IEnumerable<ExportRequest> fileExportRequests, CancellationToken cancellationToken)
{
_logger.LogDebug("Starting requesting files download...", Array.Empty<object>());
foreach (ExportRequest fileExportRequest in fileExportRequests) {
if (cancellationToken.IsCancellationRequested)
break;
try {
_logger.LogVerbose("Adding export request for downloading file for artifact {artifactId} to {destination}.", new object[2] {
fileExportRequest.ArtifactId,
fileExportRequest.DestinationLocation.Secure()
});
fileExportRequest.FileName = bridge.QueueDownload(fileExportRequest.CreateTransferPath(_safeIncrement.GetNext()));
} catch (ArgumentException ex) {
_logger.LogWarning((Exception)ex, "There was a problem downloading artifact {ArtifactId}.", new object[1] {
fileExportRequest.ArtifactId
});
_downloadProgressManager.MarkArtifactAsError(fileExportRequest.ArtifactId, ex.Message);
} catch (Exception ex2) {
_logger.LogError(ex2, "Error occurred during adding file export request to TAPI bridge. Skipping.", Array.Empty<object>());
throw;
}
}
}
}
}