diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-07-17 10:50:59 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-17 10:50:59 -0500 |
| commit | d4b0fad9cb82e70cc10a1f6189fa17c1a65c9c46 (patch) | |
| tree | 7a220a49d14498c49f14aeda78265a4fe96b5c57 /src | |
| parent | 19174cc478118010e6e39098fd2aa4e5a570a03f (diff) | |
| parent | 58626e1e8f954160fc86f4c15f437c6698bf49db (diff) | |
| download | slskd-d4b0fad9cb82e70cc10a1f6189fa17c1a65c9c46.tar.gz | |
Merge pull request #550 from slskd/search-status
Persist search status to the database as the search is running
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Search/API/Controllers/SearchesController.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Search/ISearchService.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Search/SearchService.cs | 30 |
3 files changed, 25 insertions, 9 deletions
diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index 57ddfc13..acae18e0 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -68,7 +68,7 @@ namespace slskd.Search.API try { - search = await Searches.CreateAsync(id, SearchQuery.FromText(request.SearchText), SearchScope.Network, request.ToSearchOptions()); + search = await Searches.StartAsync(id, SearchQuery.FromText(request.SearchText), SearchScope.Network, request.ToSearchOptions()); } catch (Exception ex) when (ex is ArgumentException || ex is Soulseek.DuplicateTokenException) { diff --git a/src/slskd/Search/ISearchService.cs b/src/slskd/Search/ISearchService.cs index d2cac32e..9a45280b 100644 --- a/src/slskd/Search/ISearchService.cs +++ b/src/slskd/Search/ISearchService.cs @@ -38,7 +38,7 @@ namespace slskd.Search /// <param name="scope">The search scope.</param> /// <param name="options">Search options.</param> /// <returns>The completed search.</returns> - Task<Search> CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); + Task<Search> StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); /// <summary> /// Deletes the specified ssearch. diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index f077e76b..12c75fb1 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -77,7 +77,7 @@ namespace slskd.Search /// <param name="scope">The search scope.</param> /// <param name="options">Search options.</param> /// <returns>The completed search.</returns> - public async Task<Search> CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null) + public async Task<Search> StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null) { var token = Client.GetNextToken(); @@ -100,15 +100,34 @@ 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 i/o or performance + // while searches are running should investigate this as a cause + search.ResponseCount = args.Search.ResponseCount; + search.FileCount = args.Search.FileCount; + search.LockedFileCount = args.Search.LockedFileCount; + + SearchHub.BroadcastUpdateAsync(search); + _ = UpdateAndSaveChangesAsync(search); + })); var soulseekSearchTask = Client.SearchAsync( query, @@ -132,13 +151,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>(); |
