From 5d58bc9082e0e7936e7c3aeaf3e5cd5b89f03cf3 Mon Sep 17 00:00:00 2001 From: Roger Far Date: Wed, 27 Oct 2021 18:55:12 -0600 Subject: [PATCH] Add some logging to the downloaders, fixed issue with torrents and downloads not matching up. --- CHANGELOG.md | 6 +- client/src/app/navbar/navbar.component.html | 2 +- package.json | 2 +- .../Services/Downloaders/Aria2cDownloader.cs | 78 ++++++++++++------- .../Services/Downloaders/MultiDownloader.cs | 9 +++ .../Services/Downloaders/SimpleDownloader.cs | 9 +++ .../Services/TorrentRunner.cs | 16 ++-- server/RdtClient.Web/RdtClient.Web.csproj | 2 +- 8 files changed, 85 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d228d37..b56bab2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [1.9.1] - 2021-10-24 +## [1.9.2] - 2021-10-27 +### Changed +- Fixed issue where not the correct torrent was used for a download. + +## [1.9.1] - 2021-10-27 ### Added - Added automatic torrent retrying when RealDebrid reports an error on the torrent. diff --git a/client/src/app/navbar/navbar.component.html b/client/src/app/navbar/navbar.component.html index 983ccf6..9a795e0 100644 --- a/client/src/app/navbar/navbar.component.html +++ b/client/src/app/navbar/navbar.component.html @@ -55,7 +55,7 @@ Profile Logout - + diff --git a/package.json b/package.json index 6a4ded9..4d639ad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rdt-client", - "version": "1.9.1", + "version": "1.9.2", "description": "This is a web interface to manage your torrents on Real-Debrid.", "main": "index.js", "dependencies": { diff --git a/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs b/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs index 3b6ad40..81ad9f6 100644 --- a/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs +++ b/server/RdtClient.Service/Services/Downloaders/Aria2cDownloader.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Threading.Tasks; using Aria2NET; using RdtClient.Data.Models.Internal; +using Serilog; namespace RdtClient.Service.Services.Downloaders { @@ -16,15 +17,17 @@ namespace RdtClient.Service.Services.Downloaders private const Int32 RetryCount = 5; + private readonly Aria2NetClient _aria2NetClient; + + private readonly ILogger _logger; private readonly String _uri; private readonly String _filePath; - private readonly Aria2NetClient _aria2NetClient; - private String _gid; public Aria2cDownloader(String gid, String uri, String filePath, DbSettings settings) { + _logger = Log.ForContext(); _gid = gid; _uri = uri; _filePath = filePath; @@ -42,6 +45,8 @@ namespace RdtClient.Service.Services.Downloaders var path = Path.GetDirectoryName(_filePath); var fileName = Path.GetFileName(_filePath); + _logger.Debug($"Starting download of {_uri}, writing to path: {path}, fileName: {fileName}"); + var isAlreadyAdded = await CheckIfAdded(); if (isAlreadyAdded) @@ -82,17 +87,23 @@ namespace RdtClient.Service.Services.Downloaders } }); + _logger.Debug($"Added download to Aria2, received ID {_gid}"); + await _aria2NetClient.TellStatus(_gid); + _logger.Debug($"Download with ID {_gid} found in Aria2"); + return _gid; } - catch + catch (Exception ex) { if (retryCount >= RetryCount) { throw; } + _logger.Debug($"Error starting download: {ex.Message}. Retrying {retryCount}/{RetryCount}"); + await Task.Delay(retryCount * 1000); retryCount++; @@ -102,32 +113,13 @@ namespace RdtClient.Service.Services.Downloaders public async Task Cancel() { - if (String.IsNullOrWhiteSpace(_gid)) - { - return; - } - - try - { - await _aria2NetClient.ForceRemove(_gid); - } - catch - { - // ignored - } - - try - { - await _aria2NetClient.RemoveDownloadResult(_gid); - } - catch - { - // ignored - } + await Remove(); } public async Task Pause() { + _logger.Debug($"Pausing download {_uri} {_gid}"); + if (String.IsNullOrWhiteSpace(_gid)) { return; @@ -145,6 +137,8 @@ namespace RdtClient.Service.Services.Downloaders public async Task Resume() { + _logger.Debug($"Resuming download {_uri} {_gid}"); + if (String.IsNullOrWhiteSpace(_gid)) { return; @@ -176,7 +170,7 @@ namespace RdtClient.Service.Services.Downloaders if (!String.IsNullOrWhiteSpace(download.ErrorMessage) || download.Status == "error") { - await Cancel(); + await Remove(); DownloadComplete?.Invoke(this, new DownloadCompleteEventArgs { Error = $"{download.ErrorCode}: {download.ErrorMessage}" @@ -186,7 +180,9 @@ namespace RdtClient.Service.Services.Downloaders if (download.Status == "complete" || download.Status == "removed") { - await Cancel(); + _logger.Debug($"Aria2 download found as complete {_gid}"); + + await Remove(); var retryCount = 0; while (true) @@ -220,6 +216,34 @@ namespace RdtClient.Service.Services.Downloaders }); } + private async Task Remove() + { + if (String.IsNullOrWhiteSpace(_gid)) + { + return; + } + + _logger.Debug($"Remove download {_uri} {_gid} from Aria2"); + + try + { + await _aria2NetClient.ForceRemove(_gid); + } + catch + { + // ignored + } + + try + { + await _aria2NetClient.RemoveDownloadResult(_gid); + } + catch + { + // ignored + } + } + private async Task CheckIfAdded() { var allDownloads = await _aria2NetClient.TellAll(); diff --git a/server/RdtClient.Service/Services/Downloaders/MultiDownloader.cs b/server/RdtClient.Service/Services/Downloaders/MultiDownloader.cs index 8ce5a16..1d43b59 100644 --- a/server/RdtClient.Service/Services/Downloaders/MultiDownloader.cs +++ b/server/RdtClient.Service/Services/Downloaders/MultiDownloader.cs @@ -4,6 +4,7 @@ using System.Net; using System.Threading.Tasks; using Downloader; using RdtClient.Data.Models.Internal; +using Serilog; namespace RdtClient.Service.Services.Downloaders { @@ -16,8 +17,12 @@ namespace RdtClient.Service.Services.Downloaders private readonly String _filePath; private readonly String _uri; + private readonly ILogger _logger; + public MultiDownloader(String uri, String filePath, DbSettings settings) { + _logger = Log.ForContext(); + _uri = uri; _filePath = filePath; @@ -113,6 +118,8 @@ namespace RdtClient.Service.Services.Downloaders public async Task Download() { + _logger.Debug($"Starting download of {_uri}, writing to path: {_filePath}"); + await _downloadService.DownloadFileTaskAsync(_uri, _filePath); return null; @@ -120,6 +127,8 @@ namespace RdtClient.Service.Services.Downloaders public Task Cancel() { + _logger.Debug($"Cancelling download {_uri}"); + _downloadService.CancelAsync(); return Task.CompletedTask; diff --git a/server/RdtClient.Service/Services/Downloaders/SimpleDownloader.cs b/server/RdtClient.Service/Services/Downloaders/SimpleDownloader.cs index 4268e00..ed3c908 100644 --- a/server/RdtClient.Service/Services/Downloaders/SimpleDownloader.cs +++ b/server/RdtClient.Service/Services/Downloaders/SimpleDownloader.cs @@ -2,6 +2,7 @@ using System.IO; using System.Net; using System.Threading.Tasks; +using Serilog; namespace RdtClient.Service.Services.Downloaders { @@ -22,14 +23,20 @@ namespace RdtClient.Service.Services.Downloaders private Int64 _bytesLastUpdate; private DateTime _nextUpdate; + private readonly ILogger _logger; + public SimpleDownloader(String uri, String filePath) { + _logger = Log.ForContext(); + _uri = uri; _filePath = filePath; } public Task Download() { + _logger.Debug($"Starting download of {_uri}, writing to path: {_filePath}"); + _ = Task.Run(async () => { await StartDownloadTask(); @@ -40,6 +47,8 @@ namespace RdtClient.Service.Services.Downloaders public Task Cancel() { + _logger.Debug($"Cancelling download {_uri}"); + _cancelled = true; return Task.CompletedTask; diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index ae7a836..d24eb32 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -295,10 +295,10 @@ namespace RdtClient.Service.Services foreach (var torrent in torrents) { // Check if there are any downloads that are queued and can be started. - var queuedDownloads = torrents.SelectMany(m => m.Downloads) - .Where(m => m.Completed == null && m.DownloadQueued != null && m.DownloadStarted == null && m.Error == null) - .OrderBy(m => m.DownloadQueued) - .ToList(); + var queuedDownloads = torrent.Downloads + .Where(m => m.Completed == null && m.DownloadQueued != null && m.DownloadStarted == null && m.Error == null) + .OrderBy(m => m.DownloadQueued) + .ToList(); foreach (var download in queuedDownloads) { @@ -374,10 +374,10 @@ namespace RdtClient.Service.Services } // Check if there are any unpacks that are queued and can be started. - var queuedUnpacks = torrents.SelectMany(m => m.Downloads) - .Where(m => m.Completed == null && m.UnpackingQueued != null && m.UnpackingStarted == null && m.Error == null) - .OrderBy(m => m.DownloadQueued) - .ToList(); + var queuedUnpacks = torrent.Downloads + .Where(m => m.Completed == null && m.UnpackingQueued != null && m.UnpackingStarted == null && m.Error == null) + .OrderBy(m => m.DownloadQueued) + .ToList(); foreach (var download in queuedUnpacks) { diff --git a/server/RdtClient.Web/RdtClient.Web.csproj b/server/RdtClient.Web/RdtClient.Web.csproj index 01919dc..0b6d344 100644 --- a/server/RdtClient.Web/RdtClient.Web.csproj +++ b/server/RdtClient.Web/RdtClient.Web.csproj @@ -4,7 +4,7 @@ net5.0 Exe 94c24cba-f03f-4453-a671-3640b517c573 - 1.9.1 + 1.9.2