aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-12-16 18:35:15 -0600
committerJP Dillingham <jp@dillingham.me>2024-12-16 18:35:15 -0600
commit1a0ab325c75e0f89a1afd6227402b431aeb3db32 (patch)
tree961ed4b2e9da8acf9e65c7aabcdb58ed49a6dcf4 /src
parent335e9ced13a9e4eb236438b01a24e9968a964452 (diff)
downloadslskd-1a0ab325c75e0f89a1afd6227402b431aeb3db32.tar.gz
implement search prune
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Search/SearchService.cs39
1 files changed, 36 insertions, 3 deletions
diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs
index ed661b1f..68f3e559 100644
--- a/src/slskd/Search/SearchService.cs
+++ b/src/slskd/Search/SearchService.cs
@@ -1,4 +1,4 @@
-// <copyright file="SearchService.cs" company="slskd Team">
+// <copyright file="SearchService.cs" company="slskd Team">
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
@@ -85,9 +85,8 @@ namespace slskd.Search
/// Removes <see cref="SearchStates.Completed"/> searches older than the specified <paramref name="age"/>.
/// </summary>
/// <param name="age">The age after which records are eligible for pruning, in minutes.</param>
- /// <param name="state">An optional, additional state by which records are filtered for pruning.</param>
/// <returns>The number of pruned records.</returns>
- int Prune(int age, SearchStates state = SearchStates.Completed);
+ int Prune(int age);
}
/// <summary>
@@ -310,5 +309,39 @@ namespace slskd.Search
return false;
}
+
+ /// <summary>
+ /// Removes <see cref="SearchStates.Completed"/> searches older than the specified <paramref name="age"/>.
+ /// </summary>
+ /// <param name="age">The age after which searches are eligible for pruning, in minutes.</param>
+ /// <returns>The number of pruned records.</returns>
+ public int Prune(int age)
+ {
+ try
+ {
+ using var context = ContextFactory.CreateDbContext();
+
+ var cutoffDateTime = DateTime.UtcNow.AddMinutes(-age);
+
+ // unlike other pruning operations, we don't care about state, since there's a 60 minute minimum
+ // and searches are guaranteed to be at least 60 minutes old by the time they can be pruned, they will
+ // be completed unless someone applied some rather dumb settings
+ var expired = context.Searches
+ .Where(s => !s.EndedAt.HasValue && s.EndedAt.Value < cutoffDateTime)
+ .ToList();
+
+ // also unlike other contexts, we don't soft delete searches
+ // there's no value in keeping them and because we store the responses inline, they take up a lot of space
+ context.Searches.RemoveRange(expired);
+ context.SaveChanges();
+
+ return expired.Count;
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Failed to prune searches: {Message}", ex.Message);
+ throw;
+ }
+ }
}
} \ No newline at end of file