From 71e1aae5ab23aac539a73f5f6c1f23b53090fd4e Mon Sep 17 00:00:00 2001 From: Roger Far Date: Sun, 21 Jan 2024 22:27:15 -0700 Subject: [PATCH] Force delete from activedownloads and activeunpacks when a torrent has been deleted. --- CHANGELOG.md | 4 +++ .../Services/TorrentRunner.cs | 31 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3a5fb5..76a71b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ 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). +## [2.0.60] - 2024-01-21 +### Changed +- Fixed bug where downloads could get stuck in active state while deleted. + ## [2.0.59] - 2024-01-21 ### Changed - Added the simple downloader back and moved the current internal downloader to a new setting. diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index df2b4fb..a7e32f3 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -18,14 +18,16 @@ public class TorrentRunner public static readonly ConcurrentDictionary ActiveUnpackClients = new(); private readonly ILogger _logger; + private readonly ILoggerFactory _loggerFactory; private readonly Torrents _torrents; private readonly Downloads _downloads; private readonly RemoteService _remoteService; private readonly HttpClient _httpClient; - public TorrentRunner(ILogger logger, Torrents torrents, Downloads downloads, RemoteService remoteService) + public TorrentRunner(ILogger logger, ILoggerFactory loggerFactory, Torrents torrents, Downloads downloads, RemoteService remoteService) { _logger = logger; + _loggerFactory = loggerFactory; _torrents = torrents; _downloads = downloads; _remoteService = remoteService; @@ -236,6 +238,31 @@ public class TorrentRunner var torrents = await _torrents.Get(); + // Check for deleted torrents that are stuck in the ActiveDownloads or ActiveUnpacks + foreach (var activeDownload in ActiveDownloadClients) + { + var download = torrents.SelectMany(m => m.Downloads).FirstOrDefault(m => m.DownloadId == activeDownload.Key); + + if (download == null) + { + await activeDownload.Value.Cancel(); + ActiveDownloadClients.TryRemove(activeDownload.Key, out _); + break; + } + } + + foreach (var activeUnpacks in ActiveUnpackClients) + { + var download = torrents.SelectMany(m => m.Downloads).FirstOrDefault(m => m.DownloadId == activeUnpacks.Key); + + if (download == null) + { + activeUnpacks.Value.Cancel(); + ActiveUnpackClients.TryRemove(activeUnpacks.Key, out _); + break; + } + } + // Process torrent retries foreach (var torrent in torrents.Where(m => m.Retry != null)) { @@ -357,7 +384,7 @@ public class TorrentRunner Log($"Setting download path to {downloadPath}", download, torrent); // Start the download process - var downloadClient = new DownloadClient(download, torrent, downloadPath); + var downloadClient = new DownloadClient(_loggerFactory, download, torrent, downloadPath); Log($"Starting download", download, torrent);