aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-09-17 16:18:37 -0500
committerJP Dillingham <jp@dillingham.me>2023-09-17 16:18:37 -0500
commitad3008c64a3f8c34632f570733ba133d12d2ae63 (patch)
tree4888b6be36708209c0a631cd10289633e2473fa8 /src
parent77201c8144bd88cb5a1f9cc4f5a0a939dd0c46e9 (diff)
downloadslskd-ad3008c64a3f8c34632f570733ba133d12d2ae63.tar.gz
implement download pruning
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 685bcb43..fc18b506 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -477,6 +477,47 @@ namespace slskd.Transfers.Downloads
}
/// <summary>
+ /// Removes <see cref="TransferStates.Completed"/> downloads older than the specified <paramref name="age"/>.
+ /// </summary>
+ /// <param name="age">The age after which downloads are eligible for pruning, in hours.</param>
+ /// <param name="state">An optional, additional state by which downloads are filtered for pruning.</param>
+ /// <returns>The number of pruned downloads.</returns>
+ public int Prune(int age, TransferStates state = TransferStates.Completed)
+ {
+ if (!state.HasFlag(TransferStates.Completed))
+ {
+ throw new ArgumentException($"State must include {TransferStates.Completed}", nameof(state));
+ }
+
+ try
+ {
+ using var context = ContextFactory.CreateDbContext();
+
+ var expired = context.Transfers
+ .Where(t => t.Direction == TransferDirection.Download)
+ .Where(t => !t.Removed)
+ .Where(t => t.EndedAt.HasValue && (DateTime.UtcNow - t.EndedAt.Value).Hours >= age)
+ .Where(t => t.State.HasFlag(state))
+ .ToList();
+
+ foreach (var tx in expired)
+ {
+ tx.Removed = true;
+ }
+
+ var pruned = context.SaveChanges();
+
+ Log.Debug("Pruned {Count} expired downloads with state {State}", pruned, state);
+ return pruned;
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Failed to prune downloads: {Message}", ex.Message);
+ throw;
+ }
+ }
+
+ /// <summary>
/// Removes the download matching the specified <paramref name="id"/>.
/// </summary>
/// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks>