aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-07-16 16:56:12 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-07-16 16:56:12 -0500
commitdcdec201f730d065246f1695fdde6372f71435c5 (patch)
treea23b065cd8fede3f7525813e51c00b068ce2a8b9
parent94b149fd7ab1166adfaae34aea92dbcf936af5d2 (diff)
downloadslskd-dcdec201f730d065246f1695fdde6372f71435c5.tar.gz
save search state more frequently during execution
-rw-r--r--src/slskd/Search/SearchService.cs25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs
index 94ad5e3e..45ad86db 100644
--- a/src/slskd/Search/SearchService.cs
+++ b/src/slskd/Search/SearchService.cs
@@ -100,15 +100,31 @@ namespace slskd.Search
List<SearchResponse> responses = new();
+ async Task UpdateAndSaveChangesAsync(Search search)
+ {
+ using var context = ContextFactory.CreateDbContext();
+ context.Update(search);
+ await context.SaveChangesAsync();
+ }
+
options ??= new SearchOptions();
options = options.WithActions(
stateChanged: (args) =>
{
search = search.WithSoulseekSearch(args.Search);
SearchHub.BroadcastUpdateAsync(search);
+ _ = UpdateAndSaveChangesAsync(search);
},
- responseReceived: (args) =>
- rateLimiter.Invoke(() => SearchHub.BroadcastUpdateAsync(search.WithSoulseekSearch(args.Search))));
+ responseReceived: (args) => rateLimiter.Invoke(() =>
+ {
+ // note: this is rate limited, but has the potential to update
+ // the database every 250ms (or whatever the interval is set to)
+ // for the duration of the search. any issues with disk/io or performance
+ // while searches are running should investigate this as a cause
+ search = search.WithSoulseekSearch(args.Search);
+ SearchHub.BroadcastUpdateAsync(search);
+ _ = UpdateAndSaveChangesAsync(search);
+ }));
var soulseekSearchTask = Client.SearchAsync(
query,
@@ -132,13 +148,10 @@ namespace slskd.Search
try
{
- using var context = ContextFactory.CreateDbContext();
-
search.EndedAt = DateTime.UtcNow;
search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r));
- context.Update(search);
- await context.SaveChangesAsync();
+ await UpdateAndSaveChangesAsync(search);
// zero responses before broadcasting
search.Responses = Enumerable.Empty<Response>();