diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-12 18:31:19 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-12 18:31:19 -0500 |
| commit | 460014a97bef714513564999d9955804d5ea715f (patch) | |
| tree | e1b4daa80526974311faeb890686f68749c1a54f /src | |
| parent | 99ba8a9fbc4f809041df12d1ad6334733c714144 (diff) | |
| download | slskd-460014a97bef714513564999d9955804d5ea715f.tar.gz | |
make validation method instance
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Program.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Shares/IReadOnlyShareRepository.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareScanner.cs | 13 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 20 | ||||
| -rw-r--r-- | src/slskd/Shares/SqliteShareRepository.cs | 19 |
5 files changed, 35 insertions, 26 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 55957718..44558e85 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -39,6 +39,7 @@ namespace slskd using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ApiExplorer; + using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; diff --git a/src/slskd/Shares/IReadOnlyShareRepository.cs b/src/slskd/Shares/IReadOnlyShareRepository.cs index ae2e4c6a..9822f536 100644 --- a/src/slskd/Shares/IReadOnlyShareRepository.cs +++ b/src/slskd/Shares/IReadOnlyShareRepository.cs @@ -72,5 +72,13 @@ namespace slskd.Shares /// <param name="query">The search query.</param> /// <returns>The list of matching files.</returns> IEnumerable<File> Search(SearchQuery query); + + /// <summary> + /// Attempts to validate the database at the specified <paramref name="connectionString"/>, or the + /// default <see cref="ConnectionString"/>. + /// </summary> + /// <param name="connectionString">The connection string of the database to validate.</param> + /// <returns>A value indicating whether the database is valid.</returns> + bool TryValidate(string connectionString = null); } } diff --git a/src/slskd/Shares/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs index 74f0d131..7a174396 100644 --- a/src/slskd/Shares/ShareScanner.cs +++ b/src/slskd/Shares/ShareScanner.cs @@ -65,11 +65,14 @@ namespace slskd.Shares /// </summary> /// <param name="workerCount"></param> /// <param name="soulseekFileFactory"></param> - public ShareScanner(int workerCount, ISoulseekFileFactory soulseekFileFactory = null) + public ShareScanner( + int workerCount, + IShareRepository shareRepository, + ISoulseekFileFactory soulseekFileFactory = null) { WorkerCount = workerCount; SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory(); - Repository = new ShareRepository(Program.ConnectionStrings.Shares); + Repository = shareRepository; } /// <summary> @@ -123,7 +126,7 @@ namespace slskd.Shares // it's possible that the database was tampered with between the time it was checked at startup and now // validate the tables, and if there's an issue, drop and recreate everything. - if (!ShareRepository.TryValidateDatabase(Program.ConnectionStrings.Shares)) + if (!Repository.TryValidate()) { Log.Warning("Shared file cache missing or invalid. Re-creating prior to scan."); Repository.Create(discardExisting: true); @@ -324,10 +327,6 @@ namespace slskd.Shares Repository.UpdateScan(timestamp, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); - Log.Debug("Backing up shared file cache database..."); - Repository.BackupTo(Program.ConnectionStrings.SharesBackup); - Log.Debug("Shared file cache database backup complete"); - var directoryCount = Repository.CountDirectories(); var fileCount = Repository.CountFiles(); diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index b44b79ff..8853707e 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -48,8 +48,6 @@ namespace slskd.Shares ConnectionStringFactory = shareConnectionStringFactory; CacheStorageMode = optionsMonitor.CurrentValue.Shares.Cache.StorageMode.ToEnum<StorageMode>(); - Scanner = scanner ?? new ShareScanner( - workerCount: optionsMonitor.CurrentValue.Shares.Cache.Workers); Scanner.StateMonitor.OnChange(cacheState => { @@ -82,6 +80,10 @@ namespace slskd.Shares Repository = new SqliteShareRepository(ConnectionString); + Scanner = scanner ?? new ShareScanner( + shareRepository: Repository, + workerCount: optionsMonitor.CurrentValue.Shares.Cache.Workers); + OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); @@ -258,9 +260,13 @@ namespace slskd.Shares /// </summary> /// <returns>The operation context.</returns> /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception> - public Task ScanAsync() + public async Task ScanAsync() { - return Scanner.ScanAsync(Shares, OptionsMonitor.CurrentValue.Shares); + await Scanner.ScanAsync(Shares, OptionsMonitor.CurrentValue.Shares); + + Log.Debug("Backing up shared file cache database..."); + Repository.BackupTo(BackupConnectionString); + Log.Debug("Shared file cache database backup complete"); } /// <summary> @@ -306,7 +312,7 @@ namespace slskd.Shares { Log.Information("Share cache StorageMode is 'Memory'. Attempting to load from backup..."); - if (SqliteShareRepository.TryValidateDatabase(BackupConnectionString)) + if (Repository.TryValidate(BackupConnectionString)) { Log.Information("Share cache backup validated. Attempting to restore..."); @@ -324,7 +330,7 @@ namespace slskd.Shares { Log.Information("Share cache StorageMode is 'Disk'. Attempting to validate..."); - if (SqliteShareRepository.TryValidateDatabase(ConnectionString)) + if (Repository.TryValidate(ConnectionString)) { // no-op } @@ -332,7 +338,7 @@ namespace slskd.Shares { Log.Warning("Share cache is missing, corrupt, or is out of date. Attempting to load from backup..."); - if (SqliteShareRepository.TryValidateDatabase(BackupConnectionString)) + if (Repository.TryValidate(BackupConnectionString)) { Log.Information("Share cache backup validated. Attempting to restore..."); diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs index 6a91ef82..737c6954 100644 --- a/src/slskd/Shares/SqliteShareRepository.cs +++ b/src/slskd/Shares/SqliteShareRepository.cs @@ -47,19 +47,14 @@ namespace slskd.Shares private ILogger Log { get; } = Serilog.Log.ForContext<SqliteShareRepository>(); /// <summary> - /// Attempts to validate the database at the specified <paramref name="connectionString"/>. + /// Attempts to validate the database at the specified <paramref name="connectionString"/>, or the + /// default <see cref="ConnectionString"/>. /// </summary> /// <param name="connectionString">The connection string of the database to validate.</param> /// <returns>A value indicating whether the database is valid.</returns> - /// <exception cref="ArgumentNullException">Thrown if no connection string is provided.</exception> - public static bool TryValidateDatabase(string connectionString) + public bool TryValidate(string connectionString = null) { - if (connectionString == null) - { - throw new ArgumentNullException(nameof(connectionString), "A connection string is required"); - } - - var log = Serilog.Log.ForContext<SqliteShareRepository>(); + connectionString ??= ConnectionString; // to update this schema map, run the following query against a valid, up-to-date database and paste the output below: // select '{ "' || name || '", "' || sql || '" },' from sqlite_master where type = 'table' @@ -78,7 +73,7 @@ namespace slskd.Shares try { - log.Debug("Validating shares database with connection string {String}", connectionString); + Log.Debug("Validating shares database with connection string {String}", connectionString); using var conn = new SqliteConnection(connectionString); conn.Open(); @@ -101,7 +96,7 @@ namespace slskd.Shares } else { - log.Debug("Shares database table {Table} is valid: {Actual}", table, actualSql); + Log.Debug("Shares database table {Table} is valid: {Actual}", table, actualSql); } } @@ -117,7 +112,7 @@ namespace slskd.Shares } catch (Exception ex) { - log.Debug(ex, $"Failed to validate shares database with connection string {connectionString}: {ex.Message}"); + Log.Debug(ex, $"Failed to validate shares database with connection string {connectionString}: {ex.Message}"); return false; } } |
