diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-01-27 20:37:13 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-01-27 20:37:13 -0600 |
| commit | 43abc568fecca208385db6a8c61c534dcc4b051a (patch) | |
| tree | 7c54211525d0af01e2f0cc5e89848a81f3611212 /src | |
| parent | 09a1767fb3abd3614716faa5863c26641a1aa9b9 (diff) | |
| download | slskd-43abc568fecca208385db6a8c61c534dcc4b051a.tar.gz | |
tweak sequencing of broadcasts, ensure state is set properly
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Search/SearchService.cs | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index b3487e1f..b4864918 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 @@ -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, @@ -243,6 +245,8 @@ namespace slskd.Search 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(); @@ -290,6 +294,11 @@ namespace slskd.Search var soulseekSearch = await soulseekSearchTask; search = search.WithSoulseekSearch(soulseekSearch); } + catch (Exception ex) + { + Log.Error(ex, "Failed to execute search {Search}: {Message}", new { query, scope, options }, ex.Message); + search.State = SearchStates.Completed | SearchStates.Errored; + } finally { rateLimiter.Dispose(); @@ -308,27 +317,30 @@ namespace slskd.Search } catch (Exception ex) { - // record will be left 'hanging' and will need to be cleaned up at the next boot + // 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.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; - context.Update(search); - context.SaveChanges(); + Update(search); + + await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); throw; } - - await SearchHub.BroadcastCreateAsync(search); - - return search; } /// <summary> |
