From f3a697ca6868242c9a8be7c54e109ac17ad7960e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 26 Nov 2024 17:36:53 -0800 Subject: [PATCH] Deluge communication improvements (cherry picked from commit 183b8b574a4dd948b5fada94d0e645b87710f223) --- .../Download/Clients/Deluge/Deluge.cs | 31 ++++++----- .../Download/Clients/Deluge/DelugeProxy.cs | 52 +++++++------------ 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs b/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs index 8d685ed5d..5ad0f2387 100644 --- a/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs +++ b/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs @@ -19,6 +19,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge public class Deluge : TorrentClientBase { private readonly IDelugeProxy _proxy; + private bool _hasAttemptedReconnecting; public Deluge(IDelugeProxy proxy, ITorrentFileInfoReader torrentFileInfoReader, @@ -126,14 +127,9 @@ namespace NzbDrone.Core.Download.Clients.Deluge foreach (var torrent in torrents) { - // Silently ignore torrents with no hash - if (torrent.Hash.IsNullOrWhiteSpace()) - { - continue; - } - - // Ignore torrents without a name, but track to log a single warning for all invalid torrents. - if (torrent.Name.IsNullOrWhiteSpace()) + // Ignore torrents without a hash or name, but track to log a single warning + // for all invalid torrents as well as reconnect to the Daemon. + if (torrent.Hash.IsNullOrWhiteSpace() || torrent.Name.IsNullOrWhiteSpace()) { ignoredCount++; continue; @@ -197,9 +193,20 @@ namespace NzbDrone.Core.Download.Clients.Deluge items.Add(item); } - if (ignoredCount > 0) + if (ignoredCount > 0 && _hasAttemptedReconnecting) + { + if (_hasAttemptedReconnecting) + { + _logger.Warn("{0} torrent(s) were ignored because they did not have a hash or title. Deluge may have disconnected from it's daemon. If you continue to see this error, check Deluge for invalid torrents.", ignoredCount); + } + else + { + _proxy.ReconnectToDaemon(Settings); + } + } + else { - _logger.Warn("{0} torrent(s) were ignored because they did not have a title. Check Deluge and remove any invalid torrents"); + _hasAttemptedReconnecting = false; } return items; @@ -319,9 +326,9 @@ namespace NzbDrone.Core.Download.Clients.Deluge return null; } - var enabledPlugins = _proxy.GetEnabledPlugins(Settings); + var methods = _proxy.GetMethods(Settings); - if (!enabledPlugins.Contains("Label")) + if (!methods.Any(m => m.StartsWith("label."))) { return new NzbDroneValidationFailure("MusicCategory", "Label plugin not activated") { diff --git a/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs b/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs index e6071fb53..cbe32f8e9 100644 --- a/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs @@ -18,8 +18,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge Dictionary GetConfig(DelugeSettings settings); DelugeTorrent[] GetTorrents(DelugeSettings settings); DelugeTorrent[] GetTorrentsByLabel(string label, DelugeSettings settings); - string[] GetAvailablePlugins(DelugeSettings settings); - string[] GetEnabledPlugins(DelugeSettings settings); + string[] GetMethods(DelugeSettings settings); string[] GetAvailableLabels(DelugeSettings settings); DelugeLabel GetLabelOptions(DelugeSettings settings); void SetTorrentLabel(string hash, string label, DelugeSettings settings); @@ -30,6 +29,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge string AddTorrentFromFile(string filename, byte[] fileContent, DelugeSettings settings); bool RemoveTorrent(string hash, bool removeData, DelugeSettings settings); void MoveTorrentToTopInQueue(string hash, DelugeSettings settings); + void ReconnectToDaemon(DelugeSettings settings); } public class DelugeProxy : IDelugeProxy @@ -51,25 +51,14 @@ namespace NzbDrone.Core.Download.Clients.Deluge public string GetVersion(DelugeSettings settings) { - try - { - var response = ProcessRequest(settings, "daemon.info"); + var methods = GetMethods(settings); - return response; - } - catch (DownloadClientException ex) + if (methods.Contains("daemon.get_version")) { - if (ex.Message.Contains("Unknown method")) - { - // Deluge v2 beta replaced 'daemon.info' with 'daemon.get_version'. - // It may return or become official, for now we just retry with the get_version api. - var response = ProcessRequest(settings, "daemon.get_version"); - - return response; - } - - throw; + return ProcessRequest(settings, "daemon.get_version"); } + + return ProcessRequest(settings, "daemon.info"); } public Dictionary GetConfig(DelugeSettings settings) @@ -101,6 +90,13 @@ namespace NzbDrone.Core.Download.Clients.Deluge return GetTorrents(response); } + public string[] GetMethods(DelugeSettings settings) + { + var response = ProcessRequest(settings, "system.listMethods"); + + return response; + } + public string AddTorrentFromMagnet(string magnetLink, DelugeSettings settings) { dynamic options = new ExpandoObject(); @@ -158,20 +154,6 @@ namespace NzbDrone.Core.Download.Clients.Deluge ProcessRequest(settings, "core.queue_top", (object)new string[] { hash }); } - public string[] GetAvailablePlugins(DelugeSettings settings) - { - var response = ProcessRequest(settings, "core.get_available_plugins"); - - return response; - } - - public string[] GetEnabledPlugins(DelugeSettings settings) - { - var response = ProcessRequest(settings, "core.get_enabled_plugins"); - - return response; - } - public string[] GetAvailableLabels(DelugeSettings settings) { var response = ProcessRequest(settings, "label.get_labels"); @@ -222,6 +204,12 @@ namespace NzbDrone.Core.Download.Clients.Deluge ProcessRequest(settings, "label.set_torrent", hash, label); } + public void ReconnectToDaemon(DelugeSettings settings) + { + ProcessRequest(settings, "web.disconnect"); + ConnectDaemon(BuildRequest(settings)); + } + private JsonRpcRequestBuilder BuildRequest(DelugeSettings settings) { var url = HttpRequestBuilder.BuildBaseUrl(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase);