From 71d8c3d233458fde02d7b169380ad347aaf88342 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Jan 2025 20:40:12 -0600 Subject: add a public Update() method, refactor to use it internally --- src/slskd/Search/SearchService.cs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 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 809647b2..f91714f5 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -64,6 +64,15 @@ namespace slskd.Search /// The list of searches matching the specified expression, or all searches if no expression is specified. Task> ListAsync(Expression> expression = null); + /// + /// Updates the specified . + /// + /// + /// Round-trips the database; use accordingly. + /// + /// The search to update. + void Update(Search search); + /// /// Performs a search for the specified and . /// @@ -191,6 +200,17 @@ namespace slskd.Search .ToListAsync(); } + /// + /// Updates the specified . + /// + /// The search to update. + public void Update(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Update(search); + context.SaveChanges(); + } + /// /// Performs a search for the specified and . /// @@ -223,20 +243,13 @@ namespace slskd.Search List responses = new(); - void UpdateAndSaveChanges(Search search) - { - using var context = ContextFactory.CreateDbContext(); - context.Update(search); - context.SaveChanges(); - } - options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => { search = search.WithSoulseekSearch(args.Search); SearchHub.BroadcastUpdateAsync(search); - UpdateAndSaveChanges(search); + Update(search); }, responseReceived: (args) => rateLimiter.Invoke(() => { @@ -250,7 +263,7 @@ namespace slskd.Search // 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); + Update(search); })); var soulseekSearchTask = Client.SearchAsync( @@ -278,7 +291,7 @@ namespace slskd.Search search.EndedAt = DateTime.UtcNow; search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); - UpdateAndSaveChanges(search); + Update(search); // zero responses before broadcasting, as we don't want to blast this // data out over the SignalR socket -- cgit v1.2.3 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/slskd/Search/SearchService.cs') 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/slskd/Search/SearchService.cs') 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/slskd/Search/SearchService.cs') 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/slskd/Search/SearchService.cs') 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/slskd/Search/SearchService.cs') 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