Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Refactored ArrMonitoringService and HealthCheckService as proper asp.net hosted services.
nzbdav-dev6c9dd65a143f
diff --git a/backend/Program.cs b/backend/Program.csindex 4b6da8c5..fa043433 100644--- a/backend/Program.cs+++ b/backend/Program.cs@@ -81,8 +81,8 @@ class Program.AddSingleton(websocketManager).AddSingleton<UsenetStreamingClient>().AddSingleton<QueueManager>()- .AddSingleton<ArrMonitoringService>()- .AddSingleton<HealthCheckService>()+ .AddHostedService<HealthCheckService>()+ .AddHostedService<ArrMonitoringService>().AddScoped<DavDatabaseContext>().AddScoped<DavDatabaseClient>().AddScoped<DatabaseStore>()@@ -98,12 +98,8 @@ class Program.IsWebdavAuthDisabled();});- // force instantiation of services- var app = builder.Build();- app.Services.GetRequiredService<ArrMonitoringService>();- app.Services.GetRequiredService<HealthCheckService>();-// run+ var app = builder.Build();app.UseMiddleware<ExceptionMiddleware>();app.UseWebSockets();app.MapHealthChecks("/health");diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.csindex 6476b3a3..c17c22a9 100644--- a/backend/Queue/QueueItemProcessor.cs+++ b/backend/Queue/QueueItemProcessor.cs@@ -28,7 +28,6 @@ public class QueueItemProcessor(INntpClient usenetClient,ConfigManager configManager,WebsocketManager websocketManager,- HealthCheckService healthCheckService,IProgress<int> progress,CancellationToken ct)@@ -121,7 +120,7 @@ public class QueueItemProcessor(// step 0 -- perform article existence pre-check against cache// https://github.com/nzbdav-dev/nzbdav/issues/101var articlesToPrecheck = nzbFiles.SelectMany(x => x.Segments).Select(x => x.MessageId.Value);- healthCheckService.CheckCachedMissingSegmentIds(articlesToPrecheck);+ HealthCheckService.CheckCachedMissingSegmentIds(articlesToPrecheck);// step 1 -- get name and size of each nzb filevar part1Progress = progressdiff --git a/backend/Queue/QueueManager.cs b/backend/Queue/QueueManager.csindex fa97bfd0..fafec06c 100644--- a/backend/Queue/QueueManager.cs+++ b/backend/Queue/QueueManager.cs@@ -18,7 +18,6 @@ public class QueueManager : IDisposableprivate readonly SemaphoreSlim _semaphore = new(1, 1);private readonly ConfigManager _configManager;private readonly WebsocketManager _websocketManager;- private readonly HealthCheckService _healthCheckService;private CancellationTokenSource _sleepingQueueToken = new();private readonly Lock _sleepingQueueLock = new();@@ -26,14 +25,12 @@ public class QueueManager : IDisposablepublic QueueManager(UsenetStreamingClient usenetClient,ConfigManager configManager,- WebsocketManager websocketManager,- HealthCheckService healthCheckService+ WebsocketManager websocketManager){_usenetClient = usenetClient;_configManager = configManager;_websocketManager = websocketManager;- _healthCheckService = healthCheckService;_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(SigtermUtil.GetCancellationToken());_ = ProcessQueueAsync(_cancellationTokenSource.Token);@@ -141,8 +138,7 @@ public class QueueManager : IDisposablevar progressHook = new Progress<int>();var task = new QueueItemProcessor(queueItem, queueNzbContents, dbClient, usenetClient,- _configManager, _websocketManager, _healthCheckService,- progressHook, cts.Token+ _configManager, _websocketManager, progressHook, cts.Token).ProcessAsync();var inProgressQueueItem = new InProgressQueueItem(){diff --git a/backend/Services/ArrMonitoringService.cs b/backend/Services/ArrMonitoringService.csindex c0163c57..c0652a50 100644--- a/backend/Services/ArrMonitoringService.cs+++ b/backend/Services/ArrMonitoringService.cs@@ -1,7 +1,7 @@-using NzbWebDAV.Clients.RadarrSonarr;+using Microsoft.Extensions.Hosting;+using NzbWebDAV.Clients.RadarrSonarr;using NzbWebDAV.Clients.RadarrSonarr.BaseModels;using NzbWebDAV.Config;-using NzbWebDAV.Utils;using Serilog;namespace NzbWebDAV.Services;@@ -13,23 +13,21 @@ namespace NzbWebDAV.Services;/// optionally block these stuck items, and optionally trigger a new/// search for these stuck items./// </summary>-public class ArrMonitoringService+public class ArrMonitoringService : BackgroundService{private readonly ConfigManager _configManager;- private readonly CancellationToken _cancellationToken = SigtermUtil.GetCancellationToken();public ArrMonitoringService(ConfigManager configManager){_configManager = configManager;- _ = StartMonitoringService();}- private async Task StartMonitoringService()+ protected override async Task ExecuteAsync(CancellationToken stoppingToken){- while (!_cancellationToken.IsCancellationRequested)+ while (!stoppingToken.IsCancellationRequested){// Ensure delay runs on each iteration- await Task.Delay(TimeSpan.FromSeconds(10), _cancellationToken).ConfigureAwait(false);+ await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken).ConfigureAwait(false);// if all queue-actions are disabled, then do nothingvar arrConfig = _configManager.GetArrConfig();diff --git a/backend/Services/HealthCheckService.cs b/backend/Services/HealthCheckService.csindex badd9116..bb2f34e8 100644--- a/backend/Services/HealthCheckService.cs+++ b/backend/Services/HealthCheckService.cs@@ -1,4 +1,5 @@using Microsoft.EntityFrameworkCore;+using Microsoft.Extensions.Hosting;using NzbWebDAV.Clients.Usenet;using NzbWebDAV.Config;using NzbWebDAV.Database;@@ -14,14 +15,13 @@ namespace NzbWebDAV.Services;/// <summary>/// This service monitors for health checks/// </summary>-public class HealthCheckService+public class HealthCheckService : BackgroundService{private readonly ConfigManager _configManager;private readonly INntpClient _usenetClient;private readonly WebsocketManager _websocketManager;- private readonly CancellationToken _cancellationToken = SigtermUtil.GetCancellationToken();- private readonly HashSet<string> _missingSegmentIds = [];+ private static readonly HashSet<string> _missingSegmentIds = [];public HealthCheckService(@@ -40,26 +40,24 @@ public class HealthCheckServiceif (!configEventArgs.ChangedConfig.ContainsKey("usenet.host")) return;lock (_missingSegmentIds) _missingSegmentIds.Clear();};-- _ = StartMonitoringService();}- private async Task StartMonitoringService()+ protected override async Task ExecuteAsync(CancellationToken stoppingToken){- while (!_cancellationToken.IsCancellationRequested)+ while (!stoppingToken.IsCancellationRequested){try{// if the repair-job is disabled, then don't do anythingif (!_configManager.IsRepairJobEnabled()){- await Task.Delay(TimeSpan.FromSeconds(5), _cancellationToken).ConfigureAwait(false);+ await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken).ConfigureAwait(false);continue;}// get concurrencyvar concurrency = _configManager.GetUsenetProviderConfig().TotalPooledConnections;- using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken);+ using var cts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);// get the davItem to health-checkawait using var dbContext = new DavDatabaseContext();@@ -82,7 +80,7 @@ public class HealthCheckServicecatch (Exception e){Log.Error(e, $"Unexpected error performing background health checks: {e.Message}");- await Task.Delay(TimeSpan.FromSeconds(5), _cancellationToken).ConfigureAwait(false);+ await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken).ConfigureAwait(false);}}}@@ -339,7 +337,7 @@ public class HealthCheckServicereturn result;}- public void CheckCachedMissingSegmentIds(IEnumerable<string> segmentIds)+ public static void CheckCachedMissingSegmentIds(IEnumerable<string> segmentIds){lock (_missingSegmentIds){