LongTextDownloader
using Relativity.DataExchange.Export.VolumeManagerV2.Download.TapiHelpers;
using Relativity.DataExchange.Export.VolumeManagerV2.Statistics;
using Relativity.DataExchange.Logger;
using Relativity.Logging;
using Relativity.Transfer;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Relativity.DataExchange.Export.VolumeManagerV2.Download
{
public class LongTextDownloader : ILongTextDownloader, ILongTextFileDownloadSubscriber
{
private readonly SafeIncrement _safeIncrement;
private readonly ILongTextTapiBridgePool _longTextTapiBridgePool;
private readonly ILog _logger;
private IFileDownloadSubscriber _fileDownloadSubscriber;
private readonly IDownloadProgressManager _downloadProgressManager;
public LongTextDownloader(SafeIncrement safeIncrement, ILongTextTapiBridgePool longTextTapiBridgePool, IDownloadProgressManager downloadProgressManager, ILog logger)
{
_safeIncrement = safeIncrement.ThrowIfNull("safeIncrement");
_longTextTapiBridgePool = longTextTapiBridgePool.ThrowIfNull("longTextTapiBridgePool");
_downloadProgressManager = downloadProgressManager.ThrowIfNull("downloadProgressManager");
_logger = logger.ThrowIfNull<ILog>("logger");
}
[AsyncStateMachine(typeof(<DownloadAsync>d__6))]
public Task DownloadAsync(List<LongTextExportRequest> longTextExportRequests, CancellationToken cancellationToken)
{
<DownloadAsync>d__6 stateMachine = default(<DownloadAsync>d__6);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.longTextExportRequests = longTextExportRequests;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public void RegisterSubscriber(IFileDownloadSubscriber fileDownloadSubscriber)
{
fileDownloadSubscriber.ThrowIfNull("fileDownloadSubscriber");
_fileDownloadSubscriber = fileDownloadSubscriber;
}
private void Download(List<LongTextExportRequest> longTextExportRequests, CancellationToken cancellationToken)
{
if (longTextExportRequests == null)
throw new ArgumentNullException("longTextExportRequests");
IDownloadTapiBridge downloadTapiBridge = null;
try {
downloadTapiBridge = _longTextTapiBridgePool.Request(cancellationToken);
_logger.LogDebug("Subscribing '{_fileDownloadSubscriber}' for the download event", new object[1] {
_fileDownloadSubscriber
});
_fileDownloadSubscriber?.SubscribeForDownloadEvents(downloadTapiBridge, cancellationToken);
foreach (LongTextExportRequest longTextExportRequest in longTextExportRequests) {
if (cancellationToken.IsCancellationRequested)
return;
try {
_logger.LogVerbose("Adding export request for downloading long text {fieldId} to {destination}.", new object[2] {
longTextExportRequest.FieldArtifactId,
longTextExportRequest.DestinationLocation.Secure()
});
TransferPath transferPath = longTextExportRequest.CreateTransferPath(_safeIncrement.GetNext());
longTextExportRequest.FileName = downloadTapiBridge.QueueDownload(transferPath);
} catch (ArgumentException ex) {
_logger.LogWarning((Exception)ex, "There was a problem downloading artifact {ArtifactId}.", new object[1] {
longTextExportRequest.ArtifactId
});
_downloadProgressManager.MarkArtifactAsError(longTextExportRequest.ArtifactId, ex.Message);
} catch (Exception ex2) {
_logger.LogError(ex2, "Error occurred during adding long text export request to TAPI bridge. Skipping.", Array.Empty<object>());
throw;
}
}
downloadTapiBridge.WaitForTransfers();
} finally {
if (downloadTapiBridge != null) {
_fileDownloadSubscriber?.Dispose();
_longTextTapiBridgePool.Release(downloadTapiBridge);
}
}
}
}
}