-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add qbit housekeeping service to remove stale torrents (#193)
* Add housekeeping service to clean stale torrents * version bump
- Loading branch information
1 parent
c493ef3
commit c75ecd2
Showing
4 changed files
with
67 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/qbit-collector/Features/Qbit/HousekeepingBackgroundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |