Skip to content

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

C#

Added BlobCleanupService.

nzbdav-dev73251d6ddaed
diff --git a/backend/Program.cs b/backend/Program.cs
index fa043433..dd5abaff 100644
--- a/backend/Program.cs
+++ b/backend/Program.cs
@@ -83,6 +83,7 @@ class Program
.AddSingleton<QueueManager>()
.AddHostedService<HealthCheckService>()
.AddHostedService<ArrMonitoringService>()
+ .AddHostedService<BlobCleanupService>()
.AddScoped<DavDatabaseContext>()
.AddScoped<DavDatabaseClient>()
.AddScoped<DatabaseStore>()
diff --git a/backend/Services/BlobCleanupService.cs b/backend/Services/BlobCleanupService.cs
new file mode 100644
index 00000000..dc664830
--- /dev/null
+++ b/backend/Services/BlobCleanupService.cs
@@ -0,0 +1,52 @@
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Hosting;
+using NzbWebDAV.Database;
+using Serilog;
+
+namespace NzbWebDAV.Services;
+
+/// <summary>
+/// Background service that processes the blob cleanup queue.
+/// Continuously monitors BlobCleanupQueueItems table and deletes corresponding blobs.
+/// </summary>
+public class BlobCleanupService : BackgroundService
+{
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
+ {
+ while (!stoppingToken.IsCancellationRequested)
+ {
+ try
+ {
+ await using var dbContext = new DavDatabaseContext();
+
+ // Get the first item from the queue
+ var cleanupItem = await dbContext.BlobCleanupItems
+ .FirstOrDefaultAsync(stoppingToken)
+ .ConfigureAwait(false);
+
+ // If no items in queue, wait 10 seconds before checking again
+ if (cleanupItem == null)
+ {
+ await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken).ConfigureAwait(false);
+ continue;
+ }
+
+ // Delete the blob
+ BlobStore.Delete(cleanupItem.Id);
+
+ // Remove the queue item from database
+ dbContext.BlobCleanupItems.Remove(cleanupItem);
+ await dbContext.SaveChangesAsync(stoppingToken).ConfigureAwait(false);
+
+ // Continue immediately to next iteration to process more items
+ }
+ catch (Exception e)
+ {
+ Log.Error(e, $"Error processing blob cleanup queue: {e.Message}");
+
+ // Wait 10 seconds before continuing on exception
+ await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken).ConfigureAwait(false);
+ }
+ }
+ }
+}