//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ This program is free software: you can redistribute it and/or modify
// │ it under the terms of the GNU Affero General Public License as published
// │ by the Free Software Foundation, version 3.
// │
// │ This program is distributed in the hope that it will be useful,
// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// │ GNU Affero General Public License for more details.
// │
// │ You should have received a copy of the GNU Affero General Public License
// │ along with this program. If not, see https://www.gnu.org/licenses/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
using Microsoft.Extensions.Options;
namespace slskd.Search
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Serilog;
using slskd.Search.API;
using Soulseek;
using SearchOptions = Soulseek.SearchOptions;
using SearchQuery = Soulseek.SearchQuery;
using SearchScope = Soulseek.SearchScope;
using SearchStates = Soulseek.SearchStates;
///
/// Handles the lifecycle and persistence of searches.
///
public interface ISearchService
{
///
/// Deletes the specified search.
///
/// The search to delete.
/// The operation context.
Task DeleteAsync(Search search);
///
/// Finds a single search matching the specified .
///
/// The expression to use to match searches.
/// A value indicating whether to include search responses in the result.
/// The found search, or default if not found.
/// Thrown when an expression is not supplied.
Task FindAsync(Expression> expression, bool includeResponses = false);
///
/// Returns a list of all completed and in-progress searches, with responses omitted, matching the optional .
///
/// An optional expression used to match searches.
/// 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 .
///
/// A unique identifier for the search.
/// The search query.
/// The search scope.
/// Search options.
/// The completed search.
Task StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null);
///
/// Cancels the search matching the specified , if it is in progress.
///
/// The unique identifier for the search.
/// A value indicating whether the search was successfully cancelled.
bool TryCancel(Guid id);
///
/// Removes searches older than the specified .
///
/// The age after which records are eligible for pruning, in minutes.
/// The number of pruned records.
Task PruneAsync(int age);
}
///
/// Handles the lifecycle and persistence of searches.
///
public class SearchService : ISearchService
{
///
/// Initializes a new instance of the class.
///
///
///
///
/// The database context to use.
public SearchService(
IHubContext searchHub,
IOptionsMonitor optionsMonitor,
ISoulseekClient soulseekClient,
IDbContextFactory contextFactory)
{
SearchHub = searchHub;
OptionsMonitor = optionsMonitor;
Client = soulseekClient;
ContextFactory = contextFactory;
}
private ConcurrentDictionary CancellationTokens { get; }
= new ConcurrentDictionary();
private ISoulseekClient Client { get; }
private IDbContextFactory ContextFactory { get; }
private ILogger Log { get; set; } = Serilog.Log.ForContext();
private IOptionsMonitor OptionsMonitor { get; }
private IHubContext SearchHub { get; set; }
///
/// Deletes the specified search.
///
/// The search to delete.
/// The operation context.
public Task DeleteAsync(Search search)
{
if (search == default)
{
throw new ArgumentNullException(nameof(search));
}
return DoDeleteAsync(search);
async Task DoDeleteAsync(Search search)
{
using var context = ContextFactory.CreateDbContext();
context.Searches.Remove(search);
context.SaveChanges();
await SearchHub.BroadcastDeleteAsync(search);
}
}
///
/// Finds a single search matching the specified .
///
/// The expression to use to match searches.
/// A value indicating whether to include search responses in the result.
/// The found search, or default if not found.
/// Thrown when an expression is not supplied.
public Task FindAsync(Expression> expression, bool includeResponses = false)
{
if (expression == default)
{
throw new ArgumentException("An expression must be supplied.", nameof(expression));
}
using var context = ContextFactory.CreateDbContext();
var selector = context.Searches
.AsNoTracking()
.Where(expression);
if (!includeResponses)
{
selector = selector.WithoutResponses();
}
return selector.FirstOrDefaultAsync();
}
///
/// Returns a list of all completed and in-progress searches, with responses omitted, matching the optional .
///
/// An optional expression used to match searches.
/// The list of searches matching the specified expression, or all searches if no expression is specified.
public Task> ListAsync(Expression> expression = null)
{
expression ??= s => true;
using var context = ContextFactory.CreateDbContext();
return context.Searches
.AsNoTracking()
.Where(expression)
.WithoutResponses()
.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 .
///
/// A unique identifier for the search.
/// The search query.
/// The search scope.
/// Search options.
/// The completed search.
public async Task StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null)
{
var token = Client.GetNextToken();
var cancellationTokenSource = new CancellationTokenSource();
CancellationTokens.TryAdd(id, cancellationTokenSource);
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,
Token = token,
Id = id,
State = SearchStates.Requested,
StartedAt = DateTime.UtcNow,
};
bool searchCreated = false;
bool searchBroadcasted = false;
try
{
using var context = ContextFactory.CreateDbContext();
context.Add(search);
context.SaveChanges();
searchCreated = true;
await SearchHub.BroadcastCreateAsync(search);
searchBroadcasted = true;
// initialize the list of responses that we'll use to accumulate them
// populated by the responseHandler we pass to SearchAsync
// responses arrive concurrently from many connections, so this must be thread safe
ConcurrentQueue responses = new();
options ??= new SearchOptions();
options = options.WithActions(
stateChanged: (args) =>
{
// this logic is executed by Soulseek.NET upon each transition of the searcn, including the final
// transition to Completed. if for some reason something goes wrong, we won't see the final Completed
// transition, we should instead see the task below throw, in which case it will become
// faulted and we'll set the Completed flag manually in the catch block
search = search.WithSoulseekSearch(args.Search);
Update(search);
SearchHub.BroadcastUpdateAsync(search);
Log.Debug("Search for '{Query}' state changed: {State} (id: {Id})", query, search.State, id);
},
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;
Update(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);
}));
// 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.Enqueue(response),
scope,
token,
options,
cancellationToken: cancellationTokenSource.Token);
// search 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
{
try
{
var soulseekSearch = await soulseekSearchTask;
search = search.WithSoulseekSearch(soulseekSearch);
Log.Debug("Search for '{Query}' ended normally (id: {Id})", query, id);
}
catch (Exception ex)
{
Log.Debug(ex, "Search for '{Query}' threw {Exception}: {Message} (id: {Id})", query, ex.GetType(), ex.Message, id);
// OperationCanceledException might be thrown somewhere deeper, and we don't want that to count.
// a search that was actually cancelled by the user will meet the criteria below.
if (ex is OperationCanceledException && cancellationTokenSource.Token.IsCancellationRequested)
{
Log.Information("Search for '{Query}' was cancelled", query);
search.State = SearchStates.Completed | SearchStates.Cancelled;
}
else
{
Log.Error(ex, "Failed to execute search for '{Query}': {Message}", query, ex.Message);
search.State = SearchStates.Completed | SearchStates.Errored;
}
}
// it shouldn't be possible for this to happen, as the StateChanged callback is called before the
// SearchAsync() method returns, and we will have already updated the record at that time. if for
// some extremely odd reason that doesn't happen, make sure the record is persisted the final time
// with the Completed flag set to avoid it getting "stuck"
if (!search.State.HasFlag(SearchStates.Completed))
{
search.State |= SearchStates.Completed;
}
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 = [] });
Log.Debug("Search for '{Query}' finalized (id: {Id}): {Search}", query, id, search with { Responses = [] });
Log.Information("Search for '{Query}' completed with {Responses} responses", query, search.ResponseCount);
}
catch (Exception ex)
{
// record may be left 'hanging' and will need to be cleaned up at the next boot; we tried to update but failed
Log.Error(ex, "Failed to finalize search for '{Query}': {Message}", query, ex.Message);
}
finally
{
rateLimiter.Dispose();
CancellationTokens.TryRemove(id, out _);
}
});
// broadcast and return the _newly created_ search; it will continue to be updated in the background
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);
// selectively 'undo' whatever actions we were able to take successfully
if (searchCreated)
{
search.State = SearchStates.Completed | SearchStates.Errored;
search.EndedAt = search.StartedAt;
Update(search);
if (searchBroadcasted)
{
await SearchHub.BroadcastUpdateAsync(search with { Responses = [] });
}
}
throw;
}
}
///
/// Cancels the search matching the specified , if it is in progress.
///
/// The unique identifier for the search.
/// A value indicating whether the search was successfully cancelled.
public bool TryCancel(Guid id)
{
if (CancellationTokens.TryGetValue(id, out var cts))
{
cts.Cancel(swallowObjectDisposedException: true);
return true;
}
return false;
}
///
/// Removes searches older than the specified .
///
/// The age after which searches are eligible for pruning, in minutes.
/// The number of pruned records.
public async Task PruneAsync(int age)
{
try
{
using var context = ContextFactory.CreateDbContext();
var cutoffDateTime = DateTime.UtcNow.AddMinutes(-age);
// unlike other pruning operations, we don't care about state, since there's a 60 minute minimum
// and searches are guaranteed to be at least 60 minutes old by the time they can be pruned, they will
// be completed unless someone applied some rather dumb settings
var expired = context.Searches
.Where(s => s.EndedAt.HasValue && s.EndedAt.Value < cutoffDateTime)
.WithoutResponses()
.ToList();
// defer the deletion to DeleteAsync() so that SignalR broadcasting works properly and the UI
// is updated in real time
foreach (var search in expired)
{
await DeleteAsync(search);
}
return expired.Count;
}
catch (Exception ex)
{
Log.Error(ex, "Failed to prune searches: {Message}", ex.Message);
throw;
}
}
}
}