From d16cd958001f4c9474a2bd511fb3b40a4d7c7daf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 16 Dec 2024 18:12:07 -0600 Subject: consolidate I/SearchService --- src/slskd/Search/SearchService.cs | 56 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'src/slskd/Search/SearchService.cs') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 3d2e2750..ed661b1f 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 @@ -36,6 +36,60 @@ namespace slskd.Search using SearchScope = Soulseek.SearchScope; using SearchStates = Soulseek.SearchStates; + /// + /// Handles the lifecycle and persistence of searches. + /// + public interface ISearchService + { + /// + /// Deletes the specified search. + /// + /// The search to delete. + /// The operation context. + Task DeleteAsync(Search search); + + /// + /// Finds a single search matching the specified . + /// + /// The expression to use to match searches. + /// A value indicating whether to include search responses in the result. + /// The found search, or default if not found. + /// Thrown when an expression is not supplied. + Task FindAsync(Expression> expression, bool includeResponses = false); + + /// + /// Returns a list of all completed and in-progress searches, with responses omitted, matching the optional . + /// + /// An optional expression used to match searches. + /// The list of searches matching the specified expression, or all searches if no expression is specified. + Task> ListAsync(Expression> expression = null); + + /// + /// Performs a search for the specified and . + /// + /// A unique identifier for the search. + /// The search query. + /// The search scope. + /// Search options. + /// The completed search. + Task StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); + + /// + /// Cancels the search matching the specified , if it is in progress. + /// + /// The unique identifier for the search. + /// A value indicating whether the search was successfully cancelled. + bool TryCancel(Guid id); + + /// + /// 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); + } + /// /// Handles the lifecycle and persistence of searches. /// -- cgit v1.2.3 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/slskd/Search/SearchService.cs') 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 From a2225b593955232418de262929e53a49136b6293 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 17 Dec 2024 14:31:13 -0600 Subject: fix bug in search selection, use internal DeleteAsync so deletes are broadcast --- src/slskd/Search/SearchService.cs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/slskd/Search/SearchService.cs') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 68f3e559..809647b2 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -86,7 +86,7 @@ namespace slskd.Search /// /// The age after which records are eligible for pruning, in minutes. /// The number of pruned records. - int Prune(int age); + Task PruneAsync(int age); } /// @@ -247,6 +247,8 @@ namespace slskd.Search search.FileCount = args.Search.FileCount; search.LockedFileCount = args.Search.LockedFileCount; + // note that we're not actually doing anything with the response here, that's happening in the + // response handler. we're only updating counts here. SearchHub.BroadcastUpdateAsync(search); UpdateAndSaveChanges(search); })); @@ -278,9 +280,9 @@ namespace slskd.Search UpdateAndSaveChanges(search); - // zero responses before broadcasting - search.Responses = Enumerable.Empty(); - await SearchHub.BroadcastUpdateAsync(search); + // zero responses before broadcasting, as we don't want to blast this + // data out over the SignalR socket + await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); } catch (Exception ex) { @@ -315,7 +317,7 @@ namespace slskd.Search /// /// The age after which searches are eligible for pruning, in minutes. /// The number of pruned records. - public int Prune(int age) + public async Task PruneAsync(int age) { try { @@ -327,13 +329,16 @@ namespace slskd.Search // 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) + .Where(s => s.EndedAt.HasValue && s.EndedAt.Value < cutoffDateTime) + .WithoutResponses() .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(); + // defer the deletion to DeleteAsync() so that SignalR broadcasting works properly and the UI + // is updated in real time + foreach (var search in expired) + { + await DeleteAsync(search); + } return expired.Count; } -- cgit v1.2.3