Moved the websocket update process to its own background thread to improve UI update consistency.

This commit is contained in:
Roger Far 2025-04-13 09:53:15 -06:00
parent 781937dc8f
commit a25cd62ab0
9 changed files with 69 additions and 41 deletions

View file

@ -62,12 +62,13 @@ export class TorrentFileAvailability {
} }
export enum RealDebridStatus { export enum RealDebridStatus {
NotYetAdded = -1, Queued = 0,
Processing = 0,
WaitingForFileSelection = 1, Processing = 1,
Downloading = 2, WaitingForFileSelection = 2,
Finished = 3, Downloading = 3,
Uploading = 4, Finished = 4,
Uploading = 5,
Error = 99, Error = 99,
} }

View file

@ -87,8 +87,8 @@ export class TorrentStatusPipe implements PipeTransform {
} }
switch (torrent.rdStatus) { switch (torrent.rdStatus) {
case RealDebridStatus.NotYetAdded: case RealDebridStatus.Queued:
return "Not Yet Added to Provider" return 'Not Yet Added to Provider';
case RealDebridStatus.Downloading: case RealDebridStatus.Downloading:
if (torrent.rdSeeders < 1) { if (torrent.rdSeeders < 1) {
return `Torrent stalled`; return `Torrent stalled`;

View file

@ -2,13 +2,13 @@
public enum TorrentStatus public enum TorrentStatus
{ {
NotYetAdded = -1, Queued = 0,
Processing = 0, Processing = 1,
WaitingForFileSelection = 1, WaitingForFileSelection = 2,
Downloading = 2, Downloading = 3,
Finished = 3, Finished = 4,
Uploading = 4, Uploading = 5,
Error = 99 Error = 99
} }

View file

@ -52,7 +52,7 @@ public class TaskRunner(ILogger<TaskRunner> logger, IServiceProvider serviceProv
} }
catch (Exception ex) 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); await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);

View file

@ -12,8 +12,7 @@ public class UpdateChecker(ILogger<UpdateChecker> logger) : BackgroundService
public static Boolean? IsInsecure { get; private set; } public static Boolean? IsInsecure { get; private set; }
private static readonly List<String> KnownGhsaIds = [ private static readonly List<String> KnownGhsaIds = [];
];
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {

View file

@ -152,7 +152,7 @@ public class WatchFolderChecker(ILogger<WatchFolderChecker> logger, IServiceProv
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.LogError(ex, $"Unexpected error occurred in ProviderUpdater: {ex.Message}"); logger.LogError(ex, $"Unexpected error occurred in WatchFolderChecker: {ex.Message}");
} }
} }
} }

View file

@ -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<WebsocketsUpdater> logger, IServiceProvider serviceProvider) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!Startup.Ready)
{
await Task.Delay(1000, stoppingToken);
}
var remoteService = serviceProvider.GetRequiredService<RemoteService>();
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.");
}
}

View file

@ -11,7 +11,7 @@ using System.Text.Json;
namespace RdtClient.Service.Services; namespace RdtClient.Service.Services;
public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Downloads downloads, RemoteService remoteService) public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Downloads downloads)
{ {
public static readonly ConcurrentDictionary<Guid, DownloadClient> ActiveDownloadClients = new(); public static readonly ConcurrentDictionary<Guid, DownloadClient> ActiveDownloadClients = new();
public static readonly ConcurrentDictionary<Guid, UnpackClient> ActiveUnpackClients = new(); public static readonly ConcurrentDictionary<Guid, UnpackClient> ActiveUnpackClients = new();
@ -315,12 +315,12 @@ public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Dow
} }
// Process torrents in DebridQueue // 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(); .ToList();
if (torrentsToAddToProvider.Count != 0) 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; var maxParallelDownloads = Settings.Get.Provider.MaxParallelDownloads;
@ -659,8 +659,6 @@ public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Dow
} }
} }
await remoteService.Update();
sw.Stop(); sw.Stop();
if (sw.ElapsedMilliseconds > 1000) if (sw.ElapsedMilliseconds > 1000)

View file

@ -121,7 +121,7 @@ public class Torrents(
throw new($"{ex.Message}, trying to parse {magnetLink}"); throw new($"{ex.Message}, trying to parse {magnetLink}");
} }
torrent.RdStatus = TorrentStatus.NotYetAdded; torrent.RdStatus = TorrentStatus.Queued;
torrent.RdName = magnet.Name; torrent.RdName = magnet.Name;
var hash = magnet.InfoHashes.V1OrV2.ToHex(); var hash = magnet.InfoHashes.V1OrV2.ToHex();
@ -151,7 +151,7 @@ public class Torrents(
throw new($"{ex.Message}, trying to parse {fileAsBase64}"); throw new($"{ex.Message}, trying to parse {fileAsBase64}");
} }
torrent.RdStatus = TorrentStatus.NotYetAdded; torrent.RdStatus = TorrentStatus.Queued;
torrent.RdName = monoTorrent.Name; torrent.RdName = monoTorrent.Name;
var hash = monoTorrent.InfoHashes.V1OrV2.ToHex(); var hash = monoTorrent.InfoHashes.V1OrV2.ToHex();
@ -178,17 +178,12 @@ public class Torrents(
var copyFileName = Path.Combine(Settings.Get.General.CopyAddedTorrents, FileHelper.RemoveInvalidFileNameChars(torrentName)); var copyFileName = Path.Combine(Settings.Get.General.CopyAddedTorrents, FileHelper.RemoveInvalidFileNameChars(torrentName));
switch (fileOrMagnet) copyFileName = fileOrMagnet switch
{ {
case String: String => $"{copyFileName}.magnet",
copyFileName = $"{copyFileName}.magnet"; Byte[] => $"{copyFileName}.torrent",
break; _ => throw new ArgumentException("Unexpected type for fileOrMagnet")
case Byte[]: };
copyFileName = $"{copyFileName}.torrent";
break;
default:
throw new ArgumentException("Unexpected type for fileOrMagnet");
}
if (File.Exists(copyFileName)) if (File.Exists(copyFileName))
{ {
@ -743,11 +738,11 @@ public class Torrents(
} }
var newTorrent = await torrentData.Add(null, var newTorrent = await torrentData.Add(null,
infoHash, infoHash,
fileOrMagnetContents, fileOrMagnetContents,
isFile, isFile,
torrent.DownloadClient, torrent.DownloadClient,
torrent); torrent);
return newTorrent; return newTorrent;
} }