From 5fa5f416c7fcee4a98ce41bd9f67d8261aa27301 Mon Sep 17 00:00:00 2001 From: Roger Far Date: Wed, 27 Oct 2021 21:47:22 -0600 Subject: [PATCH] Removed obsolete torrent runner code, fixed queuing issue. --- client/src/app/torrent-status.pipe.ts | 29 ++++++------ server/RdtClient.Data/Data/DownloadData.cs | 34 -------------- .../Services/Downloaders/Aria2cDownloader.cs | 11 +++-- .../RdtClient.Service/Services/Downloads.cs | 12 +---- .../Services/TorrentRunner.cs | 45 +------------------ 5 files changed, 23 insertions(+), 108 deletions(-) diff --git a/client/src/app/torrent-status.pipe.ts b/client/src/app/torrent-status.pipe.ts index 3d3cfa3..246bd96 100644 --- a/client/src/app/torrent-status.pipe.ts +++ b/client/src/app/torrent-status.pipe.ts @@ -22,7 +22,8 @@ export class TorrentStatusPipe implements PipeTransform { return 'Finished'; } - const downloading = torrent.downloads.where((m) => m.downloadStarted && !m.downloadFinished); + const downloading = torrent.downloads.where((m) => m.downloadStarted && !m.downloadFinished && m.bytesDone > 0); + const downloaded = torrent.downloads.where((m) => m.downloadFinished != null); if (downloading.length > 0) { const bytesDone = downloading.sum((m) => m.bytesDone); @@ -33,17 +34,19 @@ export class TorrentStatusPipe implements PipeTransform { progress = 0; } - let allSpeeds = downloading.sum((m) => m.speed) / downloading.length; + let allSpeeds = downloading.sum((m) => m.speed); let speed: string | string[] = '0'; - if (allSpeeds > 0) { - speed = this.pipe.transform(allSpeeds, 'filesize'); - return `Downloading file ${downloading.length}/${torrent.downloads.length} (${progress.toFixed(2)}% - ${speed}/s)`; - } + speed = this.pipe.transform(allSpeeds, 'filesize'); + + return `Downloading file ${downloading.length + downloaded.length}/${ + torrent.downloads.length + } (${progress.toFixed(2)}% - ${speed}/s)`; } - const unpacking = torrent.downloads.where((m) => m.unpackingStarted && !m.unpackingFinished); + const unpacking = torrent.downloads.where((m) => m.unpackingStarted && !m.unpackingFinished && m.bytesDone > 0); + const unpacked = torrent.downloads.where((m) => m.unpackingFinished != null); if (unpacking.length > 0) { const bytesDone = unpacking.sum((m) => m.bytesDone); @@ -54,11 +57,9 @@ export class TorrentStatusPipe implements PipeTransform { progress = 0; } - let allSpeeds = unpacking.sum((m) => m.speed) / unpacking.length; - - if (allSpeeds > 0) { - return `Extracting file ${unpacking.length}/${torrent.downloads.length} (${progress.toFixed(2)}%)`; - } + return `Extracting file ${unpacking.length + unpacked.length}/${torrent.downloads.length} (${progress.toFixed( + 2 + )}%)`; } const queuedForUnpacking = torrent.downloads.where((m) => m.unpackingQueued && !m.unpackingStarted); @@ -73,14 +74,10 @@ export class TorrentStatusPipe implements PipeTransform { return `Queued for downloading`; } - const unpacked = torrent.downloads.where((m) => m.unpackingFinished != null); - if (unpacked.length > 0) { return `Files unpacked`; } - const downloaded = torrent.downloads.where((m) => m.downloadFinished != null); - if (downloaded.length > 0) { return `Files downloaded to host`; } diff --git a/server/RdtClient.Data/Data/DownloadData.cs b/server/RdtClient.Data/Data/DownloadData.cs index cafc98a..877a800 100644 --- a/server/RdtClient.Data/Data/DownloadData.cs +++ b/server/RdtClient.Data/Data/DownloadData.cs @@ -242,40 +242,6 @@ namespace RdtClient.Data.Data await TorrentData.VoidCache(); } - public async Task Download(Guid downloadId) - { - var dbDownload = await _dataContext.Downloads - .FirstOrDefaultAsync(m => m.DownloadId == downloadId); - - dbDownload.DownloadStarted = null; - dbDownload.DownloadFinished = null; - dbDownload.UnpackingQueued = null; - dbDownload.UnpackingStarted = null; - dbDownload.UnpackingFinished = null; - dbDownload.Completed = null; - dbDownload.Error = null; - - await _dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); - } - - public async Task Unpack(Guid downloadId) - { - var dbDownload = await _dataContext.Downloads - .FirstOrDefaultAsync(m => m.DownloadId == downloadId); - - dbDownload.UnpackingQueued = DateTimeOffset.UtcNow; - dbDownload.UnpackingStarted = null; - dbDownload.UnpackingFinished = null; - dbDownload.Completed = null; - dbDownload.Error = null; - - await _dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); - } - public async Task Reset(Guid downloadId) { var dbDownload = await _dataContext.Downloads diff --git a/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs b/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs index 81ad9f6..dae8410 100644 --- a/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs +++ b/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs @@ -46,12 +46,15 @@ namespace RdtClient.Service.Services.Downloaders var fileName = Path.GetFileName(_filePath); _logger.Debug($"Starting download of {_uri}, writing to path: {path}, fileName: {fileName}"); - - var isAlreadyAdded = await CheckIfAdded(); - if (isAlreadyAdded) + if (String.IsNullOrWhiteSpace(_gid)) { - throw new Exception($"The download link {_uri} has already been added to Aria2"); + var isAlreadyAdded = await CheckIfAdded(); + + if (isAlreadyAdded) + { + throw new Exception($"The download link {_uri} has already been added to Aria2"); + } } var retryCount = 0; diff --git a/server/RdtClient.Service/Services/Downloads.cs b/server/RdtClient.Service/Services/Downloads.cs index 6ccae14..bddb8c5 100644 --- a/server/RdtClient.Service/Services/Downloads.cs +++ b/server/RdtClient.Service/Services/Downloads.cs @@ -89,17 +89,7 @@ namespace RdtClient.Service.Services { await _downloadData.DeleteForTorrent(torrentId); } - - public async Task UpdateDownload(Guid downloadId) - { - await _downloadData.Download(downloadId); - } - - public async Task UpdateUnpack(Guid downloadId) - { - await _downloadData.Unpack(downloadId); - } - + public async Task Reset(Guid downloadId) { await _downloadData.Reset(downloadId); diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index d24eb32..98cf0f1 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -179,7 +179,7 @@ namespace RdtClient.Service.Services Log($"Retrying download", download, download.Torrent); await _downloads.UpdateRetryCount(downloadId, download.RetryCount + 1); - await _downloads.UpdateDownload(downloadId); + await _downloads.Reset(downloadId); } else if (download.Torrent.RetryCount < TorrentRetryCount) { @@ -549,47 +549,6 @@ namespace RdtClient.Service.Services await _torrents.CheckForLinks(torrent.TorrentId); } - - // If the torrent has any files that need starting to be downloaded, download them. - var downloadsPending = torrent.Downloads - .Where(m => m.Completed == null && - m.DownloadStarted == null && - m.DownloadFinished == null && - m.Error == null) - .OrderBy(m => m.Added) - .ToList(); - - if (downloadsPending.Count > 0) - { - Log($"Found {downloadsPending.Count} downloads pending", torrent); - - foreach (var download in downloadsPending) - { - Log($"Marking to download", download, torrent); - - await _downloads.UpdateDownload(download.DownloadId); - } - } - - // If all files are finished downloading, move to the unpacking step. - var unpackingPending = torrent.Downloads - .Where(m => m.Completed == null && - m.DownloadFinished != null && - m.UnpackingStarted == null && - m.UnpackingFinished == null) - .ToList(); - - if (unpackingPending.Count > 0) - { - Log($"Found {unpackingPending.Count} unpacks pending", torrent); - - foreach (var download in unpackingPending) - { - Log($"Marking to unpack", download, torrent); - - await _downloads.UpdateUnpack(download.DownloadId); - } - } } // Check if torrent is complete @@ -597,7 +556,7 @@ namespace RdtClient.Service.Services { var allComplete = torrent.Downloads.Count(m => m.Completed != null); - if (allComplete > 0) + if (allComplete == torrent.Downloads.Count) { Log($"All downloads complete, marking torrent as complete", torrent);