Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Added ArrMonitoringService to handle stuck queue items.

nzbdav-devda7389a68f5f
diff --git a/backend/Program.cs b/backend/Program.cs
index 08c83a07..cbf9bcb9 100644
--- a/backend/Program.cs
+++ b/backend/Program.cs
@@ -14,6 +14,7 @@ using NzbWebDAV.Database;
using NzbWebDAV.Extensions;
using NzbWebDAV.Middlewares;
using NzbWebDAV.Queue;
+using NzbWebDAV.Services;
using NzbWebDAV.Utils;
using NzbWebDAV.WebDav;
using NzbWebDAV.WebDav.Base;
@@ -70,6 +71,7 @@ class Program
.AddSingleton(websocketManager)
.AddSingleton<UsenetStreamingClient>()
.AddSingleton<QueueManager>()
+ .AddSingleton(new ArrMonitoringService(configManager))
.AddScoped<DavDatabaseContext>()
.AddScoped<DavDatabaseClient>()
.AddScoped<DatabaseStore>()
diff --git a/backend/Services/ArrMonitoringService.cs b/backend/Services/ArrMonitoringService.cs
new file mode 100644
index 00000000..a9d5bd67
--- /dev/null
+++ b/backend/Services/ArrMonitoringService.cs
@@ -0,0 +1,78 @@
+using NzbWebDAV.Clients.RadarrSonarr;
+using NzbWebDAV.Clients.RadarrSonarr.BaseModels;
+using NzbWebDAV.Config;
+using NzbWebDAV.Utils;
+using Serilog;
+
+namespace NzbWebDAV.Services;
+
+/// <summary>
+/// - This class takes care of monitoring Radarr/Sonarr instances
+/// for stuck queue items which usually require manual intervention.
+/// - NzbDAV can be configured to automatically remove these stuck items,
+/// optionally block these stuck items, and optionally trigger a new
+/// search for these stuck items.
+/// </summary>
+public class ArrMonitoringService
+{
+ private readonly ConfigManager _configManager;
+ private readonly CancellationToken _cancellationToken = SigtermUtil.GetCancellationToken();
+
+ public ArrMonitoringService(ConfigManager configManager)
+ {
+ _configManager = configManager;
+ _ = StartMonitoringService();
+ }
+
+ private async Task StartMonitoringService()
+ {
+ while (!_cancellationToken.IsCancellationRequested)
+ {
+ var arrConfig = _configManager.GetArrConfig();
+
+ // handle stuck queue items according to the config
+ if (arrConfig.QueueRules.Any(x => x.Action != ArrConfig.QueueAction.DoNothing))
+ {
+ foreach (var radarr in arrConfig.RadarrInstances)
+ await HandleStuckQueueItems(arrConfig, new RadarrClient(radarr.Host, radarr.ApiKey));
+ foreach (var sonarr in arrConfig.SonarrInstances)
+ await HandleStuckQueueItems(arrConfig, new SonarrClient(sonarr.Host, sonarr.ApiKey));
+ }
+
+ await Task.Delay(TimeSpan.FromSeconds(10), _cancellationToken);
+ }
+ }
+
+ private async Task HandleStuckQueueItems(ArrConfig arrConfig, ArrClient client)
+ {
+ try
+ {
+ var queueStatus = await client.GetQueueStatusAsync();
+ if (queueStatus is { Warnings: false, UnknownWarnings: false }) return;
+ var queue = await client.GetQueueAsync();
+ var actionableStatuses = arrConfig.QueueRules.Select(x => x.Message);
+ var stuckRecords = queue.Records.Where(x => actionableStatuses.Any(x.HasStatusMessage));
+ foreach (var record in stuckRecords)
+ await HandleStuckQueueItem(record, arrConfig, client);
+ }
+ catch (Exception e)
+ {
+ Log.Error($"Error occured while monitoring queue for `{client.Host}`: {e.Message}");
+ }
+ }
+
+ private async Task HandleStuckQueueItem(ArrQueueRecord item, ArrConfig arrConfig, ArrClient client)
+ {
+ // since there may be multiple status messages, multiple actions may apply.
+ // in such case, always perform the strongest action.
+ var action = arrConfig.QueueRules
+ .Where(x => item.HasStatusMessage(x.Message))
+ .Select(x => x.Action)
+ .DefaultIfEmpty(ArrConfig.QueueAction.DoNothing)
+ .Max();
+
+ if (action is ArrConfig.QueueAction.DoNothing) return;
+ await client.DeleteQueueRecord(item.Id, action);
+ Log.Warning($"Resolved stuck queue item `{item.Title}` from `{client.Host}, with action `{action}`");
+ }
+}
\ No newline at end of file