Skip to content

Commit

Permalink
add qbit housekeeping service to remove stale torrents (#193)
Browse files Browse the repository at this point in the history
* Add housekeeping service to clean stale torrents

* version bump
  • Loading branch information
iPromKnight authored Mar 30, 2024
1 parent c493ef3 commit c75ecd2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
14 changes: 7 additions & 7 deletions deployment/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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:
Expand All @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions deployment/docker/src/components/knightcrawler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -40,30 +40,30 @@ 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
ports:
- "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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal static IServiceCollection AddServiceConfiguration(this IServiceCollecti
services.AddHttpClient();
services.AddSingleton<ITrackersService, TrackersService>();
services.AddHostedService<TrackersBackgroundService>();
services.AddHostedService<HousekeepingBackgroundService>();

return services;
}
Expand Down
52 changes: 52 additions & 0 deletions src/qbit-collector/Features/Qbit/HousekeepingBackgroundService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace QBitCollector.Features.Qbit;

public class HousekeepingBackgroundService(IQBittorrentClient client, ILogger<HousekeepingBackgroundService> 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.");
}
}
}

0 comments on commit c75ecd2

Please sign in to comment.