diff --git a/server/RdtClient.Data/Data/DownloadData.cs b/server/RdtClient.Data/Data/DownloadData.cs index 27f80de..6c35041 100644 --- a/server/RdtClient.Data/Data/DownloadData.cs +++ b/server/RdtClient.Data/Data/DownloadData.cs @@ -210,17 +210,27 @@ public class DownloadData await TorrentData.VoidCache(); } - public async Task UpdateRemoteId(Guid downloadId, String remoteId) + public async Task UpdateRemoteId(Guid downloadId, string remoteId) { - var dbDownload = await _dataContext.Downloads - .FirstOrDefaultAsync(m => m.DownloadId == downloadId); - - if (dbDownload == null) + await UpdateRemoteIdRange(new Dictionary { - return; - } + { downloadId, remoteId } + }); - dbDownload.RemoteId = remoteId; + } + + public async Task UpdateRemoteIdRange(Dictionary remoteIdRange) + { + foreach (var entry in remoteIdRange) + { + var dbDownload = await _dataContext.Downloads.FirstOrDefaultAsync(m => m.DownloadId == entry.Key); + if (dbDownload == null) + { + continue; + } + + dbDownload.RemoteId = entry.Value; + } await _dataContext.SaveChangesAsync(); } diff --git a/server/RdtClient.Service/Services/DownloadClient.cs b/server/RdtClient.Service/Services/DownloadClient.cs index 54d8f16..b4cddc5 100644 --- a/server/RdtClient.Service/Services/DownloadClient.cs +++ b/server/RdtClient.Service/Services/DownloadClient.cs @@ -23,7 +23,7 @@ public class DownloadClient public Int64 BytesTotal { get; private set; } public Int64 BytesDone { get; private set; } - public DownloadClient(Download download, Torrent torrent, String destinationPath) + public DownloadClient(Download download, Torrent torrent, string destinationPath) { _download = download; _torrent = torrent; @@ -54,7 +54,7 @@ public class DownloadClient await FileHelper.Delete(filePath); - Downloader = Settings.Get.DownloadClient.Client switch + Downloader = Type switch { Data.Enums.DownloadClient.Internal => new InternalDownloader(_download.Link, filePath), Data.Enums.DownloadClient.Aria2c => new Aria2cDownloader(_download.RemoteId, _download.Link, filePath), diff --git a/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs b/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs index 5f08525..909f8b3 100644 --- a/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs +++ b/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs @@ -8,9 +8,6 @@ public class SymlinkDownloader : IDownloader public event EventHandler? DownloadComplete; public event EventHandler? DownloadProgress; - private const Int32 RetryCount = 5; - private const Int32 RetryDelaySeconds = 30; - private readonly String _filePath; private readonly String _uri; @@ -26,7 +23,7 @@ public class SymlinkDownloader : IDownloader _filePath = filePath; } - public async Task Download() + public Task Download() { _logger.Debug($"Starting download of {_uri}, writing to path: {_filePath}"); @@ -39,48 +36,34 @@ public class SymlinkDownloader : IDownloader Speed = 0 }); - var retryCount = 1; - while (retryCount < RetryCount) + _logger.Debug($"Searching {Settings.Get.DownloadClient.RcloneMountPath} for {fileName}"); + + // Recursively search for the fileName in the rclone mount location. + var foundFiles = Directory.GetFiles(Settings.Get.DownloadClient.RcloneMountPath, fileName, SearchOption.AllDirectories); + + if (foundFiles.Any()) { - _logger.Debug($"Searching {Settings.Get.DownloadClient.RcloneMountPath} for {fileName} ({retryCount}/{RetryCount}) "); - - // Recursively search for the fileName in the rclone mount location. - var foundFiles = Directory.GetFiles(Settings.Get.DownloadClient.RcloneMountPath, fileName, SearchOption.AllDirectories); - - if (foundFiles.Any()) + if (foundFiles.Length > 1) { - if (foundFiles.Length > 1) - { - _logger.Warning($"Found {foundFiles.Length} files named {fileName}"); - } - - // Assume first matching filename is the one we want. - var actualFilePath = foundFiles.First(); - - var result = TryCreateSymbolicLink(actualFilePath, _filePath); - - if (result) - { - DownloadComplete?.Invoke(this, new DownloadCompleteEventArgs()); - - return actualFilePath; - } + _logger.Warning($"Found {foundFiles.Length} files named {fileName}"); } - await Task.Delay(TimeSpan.FromSeconds(30), _cancellationToken.Token); + // Assume first matching filename is the one we want. + var actualFilePath = foundFiles.First(); - retryCount++; + var result = TryCreateSymbolicLink(actualFilePath, _filePath); + + if (result) + { + DownloadComplete?.Invoke(this, new DownloadCompleteEventArgs()); + + return Task.FromResult(actualFilePath); + } } - _logger.Error($"File '{fileName}' not found after {RetryCount} attempts."); - - DownloadComplete?.Invoke(this, new DownloadCompleteEventArgs - { - Error = $"File '{fileName}' not found after {RetryCount} attempts." - }); - - return null; + // Return null and try again next cycle. + return Task.FromResult(null); } public Task Cancel() @@ -102,7 +85,7 @@ public class SymlinkDownloader : IDownloader return Task.CompletedTask; } - private Boolean TryCreateSymbolicLink(String sourcePath, String symlinkPath) + private bool TryCreateSymbolicLink(string sourcePath, string symlinkPath) { try { diff --git a/server/RdtClient.Service/Services/Downloads.cs b/server/RdtClient.Service/Services/Downloads.cs index 9acc847..efccc24 100644 --- a/server/RdtClient.Service/Services/Downloads.cs +++ b/server/RdtClient.Service/Services/Downloads.cs @@ -82,6 +82,11 @@ public class Downloads await _downloadData.UpdateRemoteId(downloadId, remoteId); } + public async Task UpdateRemoteIdRange(Dictionary updateDict) + { + await _downloadData.UpdateRemoteIdRange(updateDict); + } + public async Task DeleteForTorrent(Guid torrentId) { await _downloadData.DeleteForTorrent(torrentId); diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index 8b0d0cf..1395508 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -22,6 +22,7 @@ public class TorrentRunner private readonly Downloads _downloads; private readonly RemoteService _remoteService; private readonly HttpClient _httpClient; + private readonly Dictionary _aggregatedDownloadResults; public TorrentRunner(ILogger logger, Torrents torrents, Downloads downloads, RemoteService remoteService) { @@ -29,6 +30,7 @@ public class TorrentRunner _torrents = torrents; _downloads = downloads; _remoteService = remoteService; + _aggregatedDownloadResults = new Dictionary(); _httpClient = new HttpClient { @@ -310,10 +312,16 @@ public class TorrentRunner .OrderBy(m => m.DownloadQueued) .ToList(); + _aggregatedDownloadResults.Clear(); foreach (var download in queuedDownloads) { await ProcessDownload(download, torrent, settingDownloadPath, settingDownloadLimit); } + if (_aggregatedDownloadResults.Count > 0) + { + await _downloads.UpdateRemoteIdRange(_aggregatedDownloadResults); + + } // Check if there are any unpacks that are queued and can be started. var queuedUnpacks = torrent.Downloads @@ -633,10 +641,10 @@ public class TorrentRunner var remoteId = await downloadClient.Start(); - if (!String.IsNullOrWhiteSpace(remoteId) && download.RemoteId != remoteId) + if (!string.IsNullOrWhiteSpace(remoteId) && download.RemoteId != remoteId) { Log($"Received ID {remoteId}", download, torrent); - await _downloads.UpdateRemoteId(download.DownloadId, remoteId); + _aggregatedDownloadResults.Add(download.DownloadId, remoteId); } else {