From a25cd62ab0e369accf871e763eb2dfa2e7a25d70 Mon Sep 17 00:00:00 2001 From: Roger Far Date: Sun, 13 Apr 2025 09:53:15 -0600 Subject: [PATCH] Moved the websocket update process to its own background thread to improve UI update consistency. --- client/src/app/models/torrent.model.ts | 13 +++---- client/src/app/torrent-status.pipe.ts | 4 +-- server/RdtClient.Data/Enums/TorrentStatus.cs | 12 +++---- .../BackgroundServices/TaskRunner.cs | 2 +- .../BackgroundServices/UpdateChecker.cs | 3 +- .../BackgroundServices/WatchFolderChecker.cs | 2 +- .../BackgroundServices/WebsocketsUpdater.cs | 35 +++++++++++++++++++ .../Services/TorrentRunner.cs | 8 ++--- server/RdtClient.Service/Services/Torrents.cs | 31 +++++++--------- 9 files changed, 69 insertions(+), 41 deletions(-) create mode 100644 server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs diff --git a/client/src/app/models/torrent.model.ts b/client/src/app/models/torrent.model.ts index 0397dbb..9ac34be 100644 --- a/client/src/app/models/torrent.model.ts +++ b/client/src/app/models/torrent.model.ts @@ -62,12 +62,13 @@ export class TorrentFileAvailability { } export enum RealDebridStatus { - NotYetAdded = -1, - Processing = 0, - WaitingForFileSelection = 1, - Downloading = 2, - Finished = 3, - Uploading = 4, + Queued = 0, + + Processing = 1, + WaitingForFileSelection = 2, + Downloading = 3, + Finished = 4, + Uploading = 5, Error = 99, } diff --git a/client/src/app/torrent-status.pipe.ts b/client/src/app/torrent-status.pipe.ts index d7cf013..9d827e8 100644 --- a/client/src/app/torrent-status.pipe.ts +++ b/client/src/app/torrent-status.pipe.ts @@ -87,8 +87,8 @@ export class TorrentStatusPipe implements PipeTransform { } switch (torrent.rdStatus) { - case RealDebridStatus.NotYetAdded: - return "Not Yet Added to Provider" + case RealDebridStatus.Queued: + return 'Not Yet Added to Provider'; case RealDebridStatus.Downloading: if (torrent.rdSeeders < 1) { return `Torrent stalled`; diff --git a/server/RdtClient.Data/Enums/TorrentStatus.cs b/server/RdtClient.Data/Enums/TorrentStatus.cs index 16b556e..0d22a86 100644 --- a/server/RdtClient.Data/Enums/TorrentStatus.cs +++ b/server/RdtClient.Data/Enums/TorrentStatus.cs @@ -2,13 +2,13 @@ public enum TorrentStatus { - NotYetAdded = -1, + Queued = 0, - Processing = 0, - WaitingForFileSelection = 1, - Downloading = 2, - Finished = 3, - Uploading = 4, + Processing = 1, + WaitingForFileSelection = 2, + Downloading = 3, + Finished = 4, + Uploading = 5, Error = 99 } \ No newline at end of file diff --git a/server/RdtClient.Service/BackgroundServices/TaskRunner.cs b/server/RdtClient.Service/BackgroundServices/TaskRunner.cs index 4c0b17f..5f4769e 100644 --- a/server/RdtClient.Service/BackgroundServices/TaskRunner.cs +++ b/server/RdtClient.Service/BackgroundServices/TaskRunner.cs @@ -52,7 +52,7 @@ public class TaskRunner(ILogger logger, IServiceProvider serviceProv } catch (Exception ex) { - logger.LogError(ex, $"Unexpected error occurred in TorrentDownloadManager.Tick: {ex.Message}"); + logger.LogError(ex, $"Unexpected error occurred in TaskRunner: {ex.Message}"); } await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken); diff --git a/server/RdtClient.Service/BackgroundServices/UpdateChecker.cs b/server/RdtClient.Service/BackgroundServices/UpdateChecker.cs index 3b40085..568abb1 100644 --- a/server/RdtClient.Service/BackgroundServices/UpdateChecker.cs +++ b/server/RdtClient.Service/BackgroundServices/UpdateChecker.cs @@ -12,8 +12,7 @@ public class UpdateChecker(ILogger logger) : BackgroundService public static Boolean? IsInsecure { get; private set; } - private static readonly List KnownGhsaIds = [ - ]; + private static readonly List KnownGhsaIds = []; protected override async Task ExecuteAsync(CancellationToken stoppingToken) { diff --git a/server/RdtClient.Service/BackgroundServices/WatchFolderChecker.cs b/server/RdtClient.Service/BackgroundServices/WatchFolderChecker.cs index 4c1f243..ba369e2 100644 --- a/server/RdtClient.Service/BackgroundServices/WatchFolderChecker.cs +++ b/server/RdtClient.Service/BackgroundServices/WatchFolderChecker.cs @@ -152,7 +152,7 @@ public class WatchFolderChecker(ILogger logger, IServiceProv } catch (Exception ex) { - logger.LogError(ex, $"Unexpected error occurred in ProviderUpdater: {ex.Message}"); + logger.LogError(ex, $"Unexpected error occurred in WatchFolderChecker: {ex.Message}"); } } } diff --git a/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs b/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs new file mode 100644 index 0000000..37cf032 --- /dev/null +++ b/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs @@ -0,0 +1,35 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using RdtClient.Service.Services; + +namespace RdtClient.Service.BackgroundServices; + +public class WebsocketsUpdater(ILogger logger, IServiceProvider serviceProvider) : BackgroundService +{ + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!Startup.Ready) + { + await Task.Delay(1000, stoppingToken); + } + + var remoteService = serviceProvider.GetRequiredService(); + + while (!stoppingToken.IsCancellationRequested) + { + try + { + await remoteService.Update(); + } + catch (Exception ex) + { + logger.LogError(ex, $"Unexpected error occurred in WebsocketsUpdater: {ex.Message}"); + } + + await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken); + } + + logger.LogInformation("WebsocketsUpdater stopped."); + } +} \ No newline at end of file diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index 40f5f63..555e49f 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -11,7 +11,7 @@ using System.Text.Json; namespace RdtClient.Service.Services; -public class TorrentRunner(ILogger logger, Torrents torrents, Downloads downloads, RemoteService remoteService) +public class TorrentRunner(ILogger logger, Torrents torrents, Downloads downloads) { public static readonly ConcurrentDictionary ActiveDownloadClients = new(); public static readonly ConcurrentDictionary ActiveUnpackClients = new(); @@ -315,12 +315,12 @@ public class TorrentRunner(ILogger logger, Torrents torrents, Dow } // Process torrents in DebridQueue - var torrentsToAddToProvider = allTorrents.Where(m => m.RdId == null && m.RdAdded == null && m.FileOrMagnet != null && m.RdStatus == TorrentStatus.NotYetAdded) + var torrentsToAddToProvider = allTorrents.Where(m => m.RdId == null && m.RdAdded == null && m.FileOrMagnet != null && m.RdStatus == TorrentStatus.Queued) .ToList(); if (torrentsToAddToProvider.Count != 0) { - var downloadingTorrentsCount = allTorrents.Count(m => m.RdStatus is not (TorrentStatus.NotYetAdded or TorrentStatus.Finished or TorrentStatus.Error)); + var downloadingTorrentsCount = allTorrents.Count(m => m.RdStatus is not (TorrentStatus.Queued or TorrentStatus.Finished or TorrentStatus.Error)); var maxParallelDownloads = Settings.Get.Provider.MaxParallelDownloads; @@ -659,8 +659,6 @@ public class TorrentRunner(ILogger logger, Torrents torrents, Dow } } - await remoteService.Update(); - sw.Stop(); if (sw.ElapsedMilliseconds > 1000) diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index 85de17b..96ac155 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -121,7 +121,7 @@ public class Torrents( throw new($"{ex.Message}, trying to parse {magnetLink}"); } - torrent.RdStatus = TorrentStatus.NotYetAdded; + torrent.RdStatus = TorrentStatus.Queued; torrent.RdName = magnet.Name; var hash = magnet.InfoHashes.V1OrV2.ToHex(); @@ -151,7 +151,7 @@ public class Torrents( throw new($"{ex.Message}, trying to parse {fileAsBase64}"); } - torrent.RdStatus = TorrentStatus.NotYetAdded; + torrent.RdStatus = TorrentStatus.Queued; torrent.RdName = monoTorrent.Name; var hash = monoTorrent.InfoHashes.V1OrV2.ToHex(); @@ -178,18 +178,13 @@ public class Torrents( var copyFileName = Path.Combine(Settings.Get.General.CopyAddedTorrents, FileHelper.RemoveInvalidFileNameChars(torrentName)); - switch (fileOrMagnet) + copyFileName = fileOrMagnet switch { - case String: - copyFileName = $"{copyFileName}.magnet"; - break; - case Byte[]: - copyFileName = $"{copyFileName}.torrent"; - break; - default: - throw new ArgumentException("Unexpected type for fileOrMagnet"); - } - + String => $"{copyFileName}.magnet", + Byte[] => $"{copyFileName}.torrent", + _ => throw new ArgumentException("Unexpected type for fileOrMagnet") + }; + if (File.Exists(copyFileName)) { File.Delete(copyFileName); @@ -743,11 +738,11 @@ public class Torrents( } var newTorrent = await torrentData.Add(null, - infoHash, - fileOrMagnetContents, - isFile, - torrent.DownloadClient, - torrent); + infoHash, + fileOrMagnetContents, + isFile, + torrent.DownloadClient, + torrent); return newTorrent; }