From 1a0ab325c75e0f89a1afd6227402b431aeb3db32 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 16 Dec 2024 18:35:15 -0600 Subject: implement search prune --- src/slskd/Search/SearchService.cs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'src') 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 (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 searches older than the specified . /// /// The age after which records are eligible for pruning, in minutes. - /// An optional, additional state by which records are filtered for pruning. /// The number of pruned records. - int Prune(int age, SearchStates state = SearchStates.Completed); + int Prune(int age); } /// @@ -310,5 +309,39 @@ namespace slskd.Search return false; } + + /// + /// Removes searches older than the specified . + /// + /// The age after which searches are eligible for pruning, in minutes. + /// The number of pruned records. + 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 -- cgit v1.2.3