From bd292cf918c5ae0472fd397123ade86f318f709f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 26 Jan 2025 22:01:33 -0600 Subject: fix search record if it fails --- src/slskd/Search/SearchService.cs | 62 ++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index f91714f5..7f5597b4 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -266,7 +266,9 @@ namespace slskd.Search Update(search); })); - var soulseekSearchTask = Client.SearchAsync( + try + { + var soulseekSearchTask = Client.SearchAsync( query, responseHandler: (response) => responses.Add(response), scope, @@ -274,35 +276,47 @@ namespace slskd.Search options, cancellationToken: cancellationTokenSource.Token); - _ = Task.Run(async () => - { - try - { - var soulseekSearch = await soulseekSearchTask; - search = search.WithSoulseekSearch(soulseekSearch); - } - finally + _ = 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) + finally { - Log.Error(ex, "Failed to persist search for {SearchQuery} ({Id})", query, id); + 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) + { + Log.Error(ex, "Failed to persist search for {SearchQuery} ({Id})", query, id); + } } - } - }); + }); + } + catch (Exception ex) + { + 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(); + + throw; + } await SearchHub.BroadcastCreateAsync(search); -- cgit v1.2.3 From fa0b0a322a6d7be193400873e9fee4d9b91306d8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 27 Jan 2025 09:04:31 -0600 Subject: broadcast search created as soon as it is created --- src/slskd/Search/SearchService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 7f5597b4..c4c1003b 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 @@ -241,6 +241,8 @@ namespace slskd.Search context.Add(search); context.SaveChanges(); + await SearchHub.BroadcastCreateAsync(search); + List responses = new(); options ??= new SearchOptions(); -- cgit v1.2.3 From b9ac9709e597eb565faad43dc25757873502b31f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 27 Jan 2025 09:05:22 -0600 Subject: docs/indentation --- src/slskd/Search/SearchService.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index c4c1003b..a4b59453 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -270,14 +270,19 @@ namespace slskd.Search try { + // 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); - + 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 () => { try -- cgit v1.2.3 From 09a1767fb3abd3614716faa5863c26641a1aa9b9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 27 Jan 2025 09:05:40 -0600 Subject: docs --- src/slskd/Search/SearchService.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index a4b59453..b3487e1f 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -308,6 +308,7 @@ namespace slskd.Search } catch (Exception ex) { + // record will 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); } } -- cgit v1.2.3 From 43abc568fecca208385db6a8c61c534dcc4b051a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 27 Jan 2025 20:37:13 -0600 Subject: tweak sequencing of broadcasts, ensure state is set properly --- src/slskd/Search/SearchService.cs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src') 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 (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 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; } /// -- cgit v1.2.3 From bfda68c3aef85a9b98f346f683f75472a4f423b9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 27 Jan 2025 20:37:39 -0600 Subject: improve logging --- src/slskd/Search/API/Controllers/SearchesController.cs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') 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 OptionsSnapshot { get; } + private ILogger Log { get; set; } = Serilog.Log.ForContext(); /// /// Performs a search for the specified . @@ -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); } -- cgit v1.2.3 From daa6baca2e28b480889f7db721d5e1fbbc422723 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 27 Jan 2025 20:39:58 -0600 Subject: stub create handler --- src/web/src/components/Search/Searches.jsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/web/src/components/Search/Searches.jsx b/src/web/src/components/Search/Searches.jsx index 471f833d..362ec4fd 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) => { + console.log('search', search); + }); searchHub.onreconnecting((connectionError) => onConnectionError(connectionError?.message ?? 'Disconnected'), -- cgit v1.2.3 From 32e0b716b7cc90b727bb2901b00ad0f41234410d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 27 Jan 2025 22:15:55 -0600 Subject: update UI when 'create' is received --- src/web/src/components/Search/Searches.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/web/src/components/Search/Searches.jsx b/src/web/src/components/Search/Searches.jsx index 362ec4fd..d50462ba 100644 --- a/src/web/src/components/Search/Searches.jsx +++ b/src/web/src/components/Search/Searches.jsx @@ -73,7 +73,7 @@ const Searches = ({ server } = {}) => { }); searchHub.on('create', (search) => { - console.log('search', search); + onUpdate((old) => ({ ...old, [search.id]: search })); }); searchHub.onreconnecting((connectionError) => -- cgit v1.2.3