From 2d3ff041720f9a170c860f57aa4088016bde0b62 Mon Sep 17 00:00:00 2001 From: Flaminel Date: Wed, 26 Mar 2025 22:15:37 +0200 Subject: [PATCH] fixed Deluge failing when WebUI is not connected --- code/Common/Exceptions/FatalException.cs | 12 +++++++ .../DownloadClient/Deluge/DelugeClient.cs | 31 +++++++++++++++++-- .../DownloadClient/Deluge/DelugeService.cs | 7 ++++- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 code/Common/Exceptions/FatalException.cs diff --git a/code/Common/Exceptions/FatalException.cs b/code/Common/Exceptions/FatalException.cs new file mode 100644 index 0000000..1966150 --- /dev/null +++ b/code/Common/Exceptions/FatalException.cs @@ -0,0 +1,12 @@ +namespace Common.Exceptions; + +public class FatalException : Exception +{ + public FatalException() + { + } + + public FatalException(string message) : base(message) + { + } +} \ No newline at end of file diff --git a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs index 0f642e0..4d119ea 100644 --- a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs +++ b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs @@ -2,6 +2,7 @@ using System.Text.Json.Serialization; using Common.Configuration; using Common.Configuration.DownloadClient; +using Common.Exceptions; using Domain.Models.Deluge.Exceptions; using Domain.Models.Deluge.Request; using Domain.Models.Deluge.Response; @@ -43,9 +44,23 @@ public sealed class DelugeClient return await SendRequest("auth.login", _config.Password); } - public async Task ListMethodsAsync() + public async Task IsConnected() { - await SendRequest("system.listMethods"); + return await SendRequest("web.connected"); + } + + public async Task Connect() + { + string? firstHost = await GetHost(); + + if (string.IsNullOrEmpty(firstHost)) + { + return false; + } + + var result = await SendRequest?>("web.connect", firstHost); + + return result?.Count > 0; } public async Task Logout() @@ -53,6 +68,18 @@ public sealed class DelugeClient return await SendRequest("auth.delete_session"); } + public async Task GetHost() + { + var hosts = await SendRequest?>?>("web.get_hosts"); + + if (hosts?.Count > 1) + { + throw new FatalException("multiple Deluge hosts found - please connect to only one host"); + } + + return hosts?.FirstOrDefault()?.FirstOrDefault(); + } + public async Task> ListTorrents(Dictionary? filters = null) { filters ??= new Dictionary(); diff --git a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs index d0d88e3..b925f6a 100644 --- a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs @@ -5,6 +5,7 @@ using Common.Configuration.ContentBlocker; using Common.Configuration.DownloadCleaner; using Common.Configuration.DownloadClient; using Common.Configuration.QueueCleaner; +using Common.Exceptions; using Domain.Enums; using Domain.Models.Deluge.Response; using Infrastructure.Extensions; @@ -49,7 +50,11 @@ public class DelugeService : DownloadService, IDelugeService public override async Task LoginAsync() { await _client.LoginAsync(); - await _client.ListMethodsAsync(); + + if (!await _client.IsConnected() && !await _client.Connect()) + { + throw new FatalException("Deluge WebUI is not connected to the daemon"); + } } ///