Add some logging to the downloaders, fixed issue with torrents and downloads not matching up.

This commit is contained in:
Roger Far 2021-10-27 18:55:12 -06:00
parent d16e7f0c88
commit 5d58bc9082
8 changed files with 85 additions and 39 deletions

View file

@ -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/), 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). 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
- Added automatic torrent retrying when RealDebrid reports an error on the torrent. - Added automatic torrent retrying when RealDebrid reports an error on the torrent.

View file

@ -55,7 +55,7 @@
<a class="navbar-item"> Profile </a> <a class="navbar-item"> Profile </a>
<a class="navbar-item" (click)="logout()"> Logout </a> <a class="navbar-item" (click)="logout()"> Logout </a>
<hr class="navbar-divider" /> <hr class="navbar-divider" />
<div class="navbar-item">Version 1.9.1</div> <div class="navbar-item">Version 1.9.2</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,6 +1,6 @@
{ {
"name": "rdt-client", "name": "rdt-client",
"version": "1.9.1", "version": "1.9.2",
"description": "This is a web interface to manage your torrents on Real-Debrid.", "description": "This is a web interface to manage your torrents on Real-Debrid.",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {

View file

@ -6,6 +6,7 @@ using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Aria2NET; using Aria2NET;
using RdtClient.Data.Models.Internal; using RdtClient.Data.Models.Internal;
using Serilog;
namespace RdtClient.Service.Services.Downloaders namespace RdtClient.Service.Services.Downloaders
{ {
@ -16,15 +17,17 @@ namespace RdtClient.Service.Services.Downloaders
private const Int32 RetryCount = 5; private const Int32 RetryCount = 5;
private readonly Aria2NetClient _aria2NetClient;
private readonly ILogger _logger;
private readonly String _uri; private readonly String _uri;
private readonly String _filePath; private readonly String _filePath;
private readonly Aria2NetClient _aria2NetClient;
private String _gid; private String _gid;
public Aria2cDownloader(String gid, String uri, String filePath, DbSettings settings) public Aria2cDownloader(String gid, String uri, String filePath, DbSettings settings)
{ {
_logger = Log.ForContext<Aria2cDownloader>();
_gid = gid; _gid = gid;
_uri = uri; _uri = uri;
_filePath = filePath; _filePath = filePath;
@ -42,6 +45,8 @@ namespace RdtClient.Service.Services.Downloaders
var path = Path.GetDirectoryName(_filePath); var path = Path.GetDirectoryName(_filePath);
var fileName = Path.GetFileName(_filePath); var fileName = Path.GetFileName(_filePath);
_logger.Debug($"Starting download of {_uri}, writing to path: {path}, fileName: {fileName}");
var isAlreadyAdded = await CheckIfAdded(); var isAlreadyAdded = await CheckIfAdded();
if (isAlreadyAdded) if (isAlreadyAdded)
@ -82,17 +87,23 @@ namespace RdtClient.Service.Services.Downloaders
} }
}); });
_logger.Debug($"Added download to Aria2, received ID {_gid}");
await _aria2NetClient.TellStatus(_gid); await _aria2NetClient.TellStatus(_gid);
_logger.Debug($"Download with ID {_gid} found in Aria2");
return _gid; return _gid;
} }
catch catch (Exception ex)
{ {
if (retryCount >= RetryCount) if (retryCount >= RetryCount)
{ {
throw; throw;
} }
_logger.Debug($"Error starting download: {ex.Message}. Retrying {retryCount}/{RetryCount}");
await Task.Delay(retryCount * 1000); await Task.Delay(retryCount * 1000);
retryCount++; retryCount++;
@ -102,32 +113,13 @@ namespace RdtClient.Service.Services.Downloaders
public async Task Cancel() public async Task Cancel()
{ {
if (String.IsNullOrWhiteSpace(_gid)) await Remove();
{
return;
}
try
{
await _aria2NetClient.ForceRemove(_gid);
}
catch
{
// ignored
}
try
{
await _aria2NetClient.RemoveDownloadResult(_gid);
}
catch
{
// ignored
}
} }
public async Task Pause() public async Task Pause()
{ {
_logger.Debug($"Pausing download {_uri} {_gid}");
if (String.IsNullOrWhiteSpace(_gid)) if (String.IsNullOrWhiteSpace(_gid))
{ {
return; return;
@ -145,6 +137,8 @@ namespace RdtClient.Service.Services.Downloaders
public async Task Resume() public async Task Resume()
{ {
_logger.Debug($"Resuming download {_uri} {_gid}");
if (String.IsNullOrWhiteSpace(_gid)) if (String.IsNullOrWhiteSpace(_gid))
{ {
return; return;
@ -176,7 +170,7 @@ namespace RdtClient.Service.Services.Downloaders
if (!String.IsNullOrWhiteSpace(download.ErrorMessage) || download.Status == "error") if (!String.IsNullOrWhiteSpace(download.ErrorMessage) || download.Status == "error")
{ {
await Cancel(); await Remove();
DownloadComplete?.Invoke(this, new DownloadCompleteEventArgs DownloadComplete?.Invoke(this, new DownloadCompleteEventArgs
{ {
Error = $"{download.ErrorCode}: {download.ErrorMessage}" Error = $"{download.ErrorCode}: {download.ErrorMessage}"
@ -186,7 +180,9 @@ namespace RdtClient.Service.Services.Downloaders
if (download.Status == "complete" || download.Status == "removed") if (download.Status == "complete" || download.Status == "removed")
{ {
await Cancel(); _logger.Debug($"Aria2 download found as complete {_gid}");
await Remove();
var retryCount = 0; var retryCount = 0;
while (true) 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<Boolean> CheckIfAdded() private async Task<Boolean> CheckIfAdded()
{ {
var allDownloads = await _aria2NetClient.TellAll(); var allDownloads = await _aria2NetClient.TellAll();

View file

@ -4,6 +4,7 @@ using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Downloader; using Downloader;
using RdtClient.Data.Models.Internal; using RdtClient.Data.Models.Internal;
using Serilog;
namespace RdtClient.Service.Services.Downloaders namespace RdtClient.Service.Services.Downloaders
{ {
@ -16,8 +17,12 @@ namespace RdtClient.Service.Services.Downloaders
private readonly String _filePath; private readonly String _filePath;
private readonly String _uri; private readonly String _uri;
private readonly ILogger _logger;
public MultiDownloader(String uri, String filePath, DbSettings settings) public MultiDownloader(String uri, String filePath, DbSettings settings)
{ {
_logger = Log.ForContext<MultiDownloader>();
_uri = uri; _uri = uri;
_filePath = filePath; _filePath = filePath;
@ -113,6 +118,8 @@ namespace RdtClient.Service.Services.Downloaders
public async Task<String> Download() public async Task<String> Download()
{ {
_logger.Debug($"Starting download of {_uri}, writing to path: {_filePath}");
await _downloadService.DownloadFileTaskAsync(_uri, _filePath); await _downloadService.DownloadFileTaskAsync(_uri, _filePath);
return null; return null;
@ -120,6 +127,8 @@ namespace RdtClient.Service.Services.Downloaders
public Task Cancel() public Task Cancel()
{ {
_logger.Debug($"Cancelling download {_uri}");
_downloadService.CancelAsync(); _downloadService.CancelAsync();
return Task.CompletedTask; return Task.CompletedTask;

View file

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Serilog;
namespace RdtClient.Service.Services.Downloaders namespace RdtClient.Service.Services.Downloaders
{ {
@ -22,14 +23,20 @@ namespace RdtClient.Service.Services.Downloaders
private Int64 _bytesLastUpdate; private Int64 _bytesLastUpdate;
private DateTime _nextUpdate; private DateTime _nextUpdate;
private readonly ILogger _logger;
public SimpleDownloader(String uri, String filePath) public SimpleDownloader(String uri, String filePath)
{ {
_logger = Log.ForContext<Aria2cDownloader>();
_uri = uri; _uri = uri;
_filePath = filePath; _filePath = filePath;
} }
public Task<String> Download() public Task<String> Download()
{ {
_logger.Debug($"Starting download of {_uri}, writing to path: {_filePath}");
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {
await StartDownloadTask(); await StartDownloadTask();
@ -40,6 +47,8 @@ namespace RdtClient.Service.Services.Downloaders
public Task Cancel() public Task Cancel()
{ {
_logger.Debug($"Cancelling download {_uri}");
_cancelled = true; _cancelled = true;
return Task.CompletedTask; return Task.CompletedTask;

View file

@ -295,10 +295,10 @@ namespace RdtClient.Service.Services
foreach (var torrent in torrents) foreach (var torrent in torrents)
{ {
// Check if there are any downloads that are queued and can be started. // Check if there are any downloads that are queued and can be started.
var queuedDownloads = torrents.SelectMany(m => m.Downloads) var queuedDownloads = torrent.Downloads
.Where(m => m.Completed == null && m.DownloadQueued != null && m.DownloadStarted == null && m.Error == null) .Where(m => m.Completed == null && m.DownloadQueued != null && m.DownloadStarted == null && m.Error == null)
.OrderBy(m => m.DownloadQueued) .OrderBy(m => m.DownloadQueued)
.ToList(); .ToList();
foreach (var download in queuedDownloads) 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. // Check if there are any unpacks that are queued and can be started.
var queuedUnpacks = torrents.SelectMany(m => m.Downloads) var queuedUnpacks = torrent.Downloads
.Where(m => m.Completed == null && m.UnpackingQueued != null && m.UnpackingStarted == null && m.Error == null) .Where(m => m.Completed == null && m.UnpackingQueued != null && m.UnpackingStarted == null && m.Error == null)
.OrderBy(m => m.DownloadQueued) .OrderBy(m => m.DownloadQueued)
.ToList(); .ToList();
foreach (var download in queuedUnpacks) foreach (var download in queuedUnpacks)
{ {

View file

@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<UserSecretsId>94c24cba-f03f-4453-a671-3640b517c573</UserSecretsId> <UserSecretsId>94c24cba-f03f-4453-a671-3640b517c573</UserSecretsId>
<Version>1.9.1</Version> <Version>1.9.2</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>