diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-09-17 16:12:11 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-09-17 16:12:11 -0500 |
| commit | 38b5449d622e8d7a12708a182a7498ad955654a3 (patch) | |
| tree | d6fb90783176b6220c2c625495b4ea0bc0d4712f /src | |
| parent | 3920f14a70107f67831d09934861badbc1fc70cf (diff) | |
| download | slskd-38b5449d622e8d7a12708a182a7498ad955654a3.tar.gz | |
implement upload pruning
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index b9f74dac..e8393a1a 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -453,6 +453,47 @@ namespace slskd.Transfers.Uploads } /// <summary> + /// Removes <see cref="TransferStates.Completed"/> uploads older than the specified <paramref name="age"/>. + /// </summary> + /// <param name="age">The age after which uploads are eligible for pruning, in hours.</param> + /// <param name="state">An optional, additional state by which uploads are filtered for pruning.</param> + /// <returns>The number of pruned uploads.</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.Upload) + .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 uploads with state {State}", pruned, state); + return pruned; + } + catch (Exception ex) + { + Log.Error(ex, "Failed to prune uploads: {Message}", ex.Message); + throw; + } + } + + /// <summary> /// Removes the upload matching the specified <paramref name="id"/>. /// </summary> /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> |
