diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-01-29 23:28:57 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-29 23:28:57 -0600 |
| commit | a2e1e65b7f8500345139b15e2646c27e729a4abf (patch) | |
| tree | 13c7ef7626bdbfb2201433ebb415ff77ce73264f /src | |
| parent | f3b79d3b4e4c79f77f5a9f87f06304ba1222dd12 (diff) | |
| parent | 32e0b716b7cc90b727bb2901b00ad0f41234410d (diff) | |
| download | slskd-a2e1e65b7f8500345139b15e2646c27e729a4abf.tar.gz | |
Merge pull request #1286 from slskd/search
Fix 'stuck' searches, including those that contain a single letter
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Search/API/Controllers/SearchesController.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Search/SearchService.cs | 96 | ||||
| -rw-r--r-- | src/web/src/components/Search/Searches.jsx | 4 |
3 files changed, 77 insertions, 32 deletions
diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index d6d96bbe..58e1fe37 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -24,6 +24,7 @@ namespace slskd.Search.API using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using Serilog; using SearchQuery = Soulseek.SearchQuery; using SearchScope = Soulseek.SearchScope; @@ -50,6 +51,7 @@ namespace slskd.Search.API private ISearchService Searches { get; } private IOptionsSnapshot<Options> OptionsSnapshot { get; } + private ILogger Log { get; set; } = Serilog.Log.ForContext<SearchesController>(); /// <summary> /// Performs a search for the specified <paramref name="request"/>. @@ -83,12 +85,19 @@ namespace slskd.Search.API } catch (Exception ex) when (ex is ArgumentException || ex is Soulseek.DuplicateTokenException) { + Log.Error(ex, "Failed to execute search {Search}: {Message}", request, ex.Message); return BadRequest(ex.Message); } catch (InvalidOperationException ex) { + Log.Error(ex, "Failed to execute search {Search}: {Message}", request, ex.Message); return Conflict(ex.Message); } + catch (Exception ex) + { + Log.Error(ex, "Failed to execute search {Search}: {Message}", request, ex.Message); + return StatusCode(500, ex.Message); + } return Ok(search); } diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index f91714f5..b4864918 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -228,6 +228,8 @@ namespace slskd.Search var rateLimiter = new RateLimiter(250); + // initialize the search record, save it to the database, and broadcast the creation + // we do this so the UI has some feedback to show to the user that we've gotten their request var search = new Search() { SearchText = query.SearchText, @@ -241,6 +243,10 @@ namespace slskd.Search context.Add(search); context.SaveChanges(); + await SearchHub.BroadcastCreateAsync(search); + + // initialize the list of responses that we'll use to accumulate them + // populated by the responseHandler we pass to SearchAsync List<SearchResponse> responses = new(); options ??= new SearchOptions(); @@ -266,47 +272,75 @@ namespace slskd.Search Update(search); })); - var soulseekSearchTask = Client.SearchAsync( - query, - responseHandler: (response) => responses.Add(response), - scope, - token, - options, - cancellationToken: cancellationTokenSource.Token); - - _ = Task.Run(async () => + try { - try - { - var soulseekSearch = await soulseekSearchTask; - search = search.WithSoulseekSearch(soulseekSearch); - } - finally + // initiate the search. this can throw at invocation if there's a problem with + // the client state (e.g. disconnected) or a problem with the search (e.g. no terms) + var soulseekSearchTask = Client.SearchAsync( + query, + responseHandler: (response) => responses.Add(response), + scope, + token, + options, + cancellationToken: cancellationTokenSource.Token); + + // seach looks ok so far; let the rest of the logic run asynchronously + // on a background thread. this logic needs to clean up after itself and + // update the search record to accurately reflect the final state + _ = Task.Run(async () => { - rateLimiter.Dispose(); - CancellationTokens.TryRemove(id, out _); - try { - search.EndedAt = DateTime.UtcNow; - search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); - - Update(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 = [] }); + var soulseekSearch = await soulseekSearchTask; + search = search.WithSoulseekSearch(soulseekSearch); } catch (Exception ex) { - Log.Error(ex, "Failed to persist search for {SearchQuery} ({Id})", query, id); + Log.Error(ex, "Failed to execute search {Search}: {Message}", new { query, scope, options }, ex.Message); + search.State = SearchStates.Completed | SearchStates.Errored; } - } - }); + finally + { + rateLimiter.Dispose(); + CancellationTokens.TryRemove(id, out _); + + try + { + search.EndedAt = DateTime.UtcNow; + search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); + + Update(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) + { + // record may be left 'hanging' and will need to be cleaned up at the next boot + Log.Error(ex, "Failed to persist search for {SearchQuery} ({Id})", query, id); + } + } + }); - await SearchHub.BroadcastCreateAsync(search); + await SearchHub.BroadcastUpdateAsync(search); + + return search; + } + catch (Exception ex) + { + // we'll end up here if the initial call throws for an ArgumentException, InvalidOperationException if + // the app isn't connected, and a few other straightforward issues that arise before even requesting the search + Log.Error(ex, "Failed to execute search {Search}: {Message}", new { query, scope, options }, ex.Message); + + search.State = SearchStates.Completed | SearchStates.Errored; + search.EndedAt = search.StartedAt; + Update(search); + + await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); - return search; + throw; + } } /// <summary> diff --git a/src/web/src/components/Search/Searches.jsx b/src/web/src/components/Search/Searches.jsx index 471f833d..d50462ba 100644 --- a/src/web/src/components/Search/Searches.jsx +++ b/src/web/src/components/Search/Searches.jsx @@ -72,7 +72,9 @@ const Searches = ({ server } = {}) => { }); }); - searchHub.on('create', () => {}); + searchHub.on('create', (search) => { + onUpdate((old) => ({ ...old, [search.id]: search })); + }); searchHub.onreconnecting((connectionError) => onConnectionError(connectionError?.message ?? 'Disconnected'), |
