diff --git a/deployment/docker/docker-compose.yaml b/deployment/docker/docker-compose.yaml index eeae9ae..17c8e1b 100644 --- a/deployment/docker/docker-compose.yaml +++ b/deployment/docker/docker-compose.yaml @@ -94,7 +94,7 @@ services: condition: service_healthy env_file: stack.env hostname: knightcrawler-addon - image: gabisonfire/knightcrawler-addon:2.0.16 + image: gabisonfire/knightcrawler-addon:2.0.17 labels: logging: promtail networks: @@ -117,7 +117,7 @@ services: redis: condition: service_healthy env_file: stack.env - image: gabisonfire/knightcrawler-consumer:2.0.16 + image: gabisonfire/knightcrawler-consumer:2.0.17 labels: logging: promtail networks: @@ -138,7 +138,7 @@ services: redis: condition: service_healthy env_file: stack.env - image: gabisonfire/knightcrawler-debrid-collector:2.0.16 + image: gabisonfire/knightcrawler-debrid-collector:2.0.17 labels: logging: promtail networks: @@ -152,7 +152,7 @@ services: migrator: condition: service_completed_successfully env_file: stack.env - image: gabisonfire/knightcrawler-metadata:2.0.16 + image: gabisonfire/knightcrawler-metadata:2.0.17 networks: - knightcrawler-network restart: "no" @@ -163,7 +163,7 @@ services: postgres: condition: service_healthy env_file: stack.env - image: gabisonfire/knightcrawler-migrator:2.0.16 + image: gabisonfire/knightcrawler-migrator:2.0.17 networks: - knightcrawler-network restart: "no" @@ -182,7 +182,7 @@ services: redis: condition: service_healthy env_file: stack.env - image: gabisonfire/knightcrawler-producer:2.0.16 + image: gabisonfire/knightcrawler-producer:2.0.17 labels: logging: promtail networks: @@ -207,7 +207,7 @@ services: deploy: replicas: ${QBIT_REPLICAS:-0} env_file: stack.env - image: gabisonfire/knightcrawler-qbit-collector:2.0.16 + image: gabisonfire/knightcrawler-qbit-collector:2.0.17 labels: logging: promtail networks: diff --git a/deployment/docker/src/components/knightcrawler.yaml b/deployment/docker/src/components/knightcrawler.yaml index e0e3a66..3441e69 100644 --- a/deployment/docker/src/components/knightcrawler.yaml +++ b/deployment/docker/src/components/knightcrawler.yaml @@ -20,7 +20,7 @@ x-depends: &knightcrawler-app-depends services: metadata: - image: gabisonfire/knightcrawler-metadata:2.0.16 + image: gabisonfire/knightcrawler-metadata:2.0.17 env_file: ../../.env networks: - knightcrawler-network @@ -30,7 +30,7 @@ services: condition: service_completed_successfully migrator: - image: gabisonfire/knightcrawler-migrator:2.0.16 + image: gabisonfire/knightcrawler-migrator:2.0.17 env_file: ../../.env networks: - knightcrawler-network @@ -40,7 +40,7 @@ services: condition: service_healthy addon: - image: gabisonfire/knightcrawler-addon:2.0.16 + image: gabisonfire/knightcrawler-addon:2.0.17 <<: [*knightcrawler-app, *knightcrawler-app-depends] restart: unless-stopped hostname: knightcrawler-addon @@ -48,22 +48,22 @@ services: - "7000:7000" consumer: - image: gabisonfire/knightcrawler-consumer:2.0.16 + image: gabisonfire/knightcrawler-consumer:2.0.17 <<: [*knightcrawler-app, *knightcrawler-app-depends] restart: unless-stopped debridcollector: - image: gabisonfire/knightcrawler-debrid-collector:2.0.16 + image: gabisonfire/knightcrawler-debrid-collector:2.0.17 <<: [*knightcrawler-app, *knightcrawler-app-depends] restart: unless-stopped producer: - image: gabisonfire/knightcrawler-producer:2.0.16 + image: gabisonfire/knightcrawler-producer:2.0.17 <<: [*knightcrawler-app, *knightcrawler-app-depends] restart: unless-stopped qbitcollector: - image: gabisonfire/knightcrawler-qbit-collector:2.0.16 + image: gabisonfire/knightcrawler-qbit-collector:2.0.17 <<: [*knightcrawler-app, *knightcrawler-app-depends] restart: unless-stopped depends_on: diff --git a/src/qbit-collector/Extensions/ServiceCollectionExtensions.cs b/src/qbit-collector/Extensions/ServiceCollectionExtensions.cs index 3f5c347..3f3f41a 100644 --- a/src/qbit-collector/Extensions/ServiceCollectionExtensions.cs +++ b/src/qbit-collector/Extensions/ServiceCollectionExtensions.cs @@ -18,6 +18,7 @@ public static class ServiceCollectionExtensions services.AddHttpClient(); services.AddSingleton(); services.AddHostedService(); + services.AddHostedService(); return services; } diff --git a/src/qbit-collector/Features/Qbit/HousekeepingBackgroundService.cs b/src/qbit-collector/Features/Qbit/HousekeepingBackgroundService.cs new file mode 100644 index 0000000..f01b8f2 --- /dev/null +++ b/src/qbit-collector/Features/Qbit/HousekeepingBackgroundService.cs @@ -0,0 +1,52 @@ +namespace QBitCollector.Features.Qbit; + +public class HousekeepingBackgroundService(IQBittorrentClient client, ILogger logger) : BackgroundService +{ + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + logger.LogInformation("Service is Running."); + + await DoWork(); + + using PeriodicTimer timer = new(TimeSpan.FromMinutes(2)); + + try + { + while (await timer.WaitForNextTickAsync(stoppingToken)) + { + await DoWork(); + } + } + catch (OperationCanceledException) + { + logger.LogInformation("Service stopping."); + } + } + + private async Task DoWork() + { + try + { + logger.LogInformation("Cleaning Stale Entries in Qbit..."); + + var torrents = await client.GetTorrentListAsync(); + + foreach (var torrentInfo in torrents) + { + if (!(torrentInfo.AddedOn < DateTimeOffset.UtcNow.AddMinutes(-1))) + { + continue; + } + + logger.LogInformation("Torrent [{InfoHash}] Identified as stale because was added at {AddedOn}", torrentInfo.Hash, torrentInfo.AddedOn); + + await client.DeleteAsync(new[] {torrentInfo.Hash}, deleteDownloadedData: true); + logger.LogInformation("Cleaned up stale torrent: [{InfoHash}]", torrentInfo.Hash); + } + } + catch (Exception e) + { + logger.LogError(e, "Error cleaning up stale torrents this interval."); + } + } +} \ No newline at end of file