diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-09-28 10:48:48 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-09-28 10:48:48 -0500 |
| commit | bf3e8790af148e5c9cb48ebc16308ce926dba8f5 (patch) | |
| tree | 00ee8035d388a35b4fd36a70119738fb074b63b0 /src | |
| parent | 5e0fd03418b9093b0a8c2fce7f8cce8010defc87 (diff) | |
| download | slskd-bf3e8790af148e5c9cb48ebc16308ce926dba8f5.tar.gz | |
remove loading from scanner
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Shares/IShareService.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareScanner.cs | 64 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 58 |
4 files changed, 57 insertions, 69 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index f517850b..7e9ad4fd 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -373,7 +373,7 @@ namespace slskd // try to load the share cache from disk. // if not successful, the file is missing one or more tables, or is not present at all - if (!await Shares.TryLoadFromDiskAsync()) + if (!Shares.TryLoadFromDisk()) { Log.Warning("Unable to load share cache from disk. A share scan is required."); diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index 73f6a4ad..429ab529 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -93,6 +93,6 @@ namespace slskd.Shares /// Attempt to load shares from disk. /// </summary> /// <returns>A value indicating whether shares were loaded.</returns> - Task<bool> TryLoadFromDiskAsync(); + bool TryLoadFromDisk(); } }
\ No newline at end of file diff --git a/src/slskd/Shares/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs index 01ccf4a1..03b9cae5 100644 --- a/src/slskd/Shares/ShareScanner.cs +++ b/src/slskd/Shares/ShareScanner.cs @@ -53,12 +53,6 @@ namespace slskd.Shares /// </summary> /// <returns>A value indicating whether a fill operation was cancelled.</returns> bool TryCancelScan(); - - /// <summary> - /// Attempts to load the cache from disk. - /// </summary> - /// <returns>A value indicating whether the load was successful.</returns> - bool TryLoad(); } /// <summary> @@ -69,12 +63,10 @@ namespace slskd.Shares /// <summary> /// Initializes a new instance of the <see cref="ShareScanner"/> class. /// </summary> - /// <param name="storageMode"></param> /// <param name="workerCount"></param> /// <param name="soulseekFileFactory"></param> - public ShareScanner(StorageMode storageMode, int workerCount, ISoulseekFileFactory soulseekFileFactory = null) + public ShareScanner(int workerCount, ISoulseekFileFactory soulseekFileFactory = null) { - StorageMode = storageMode; WorkerCount = workerCount; SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory(); Repository = new ShareRepository(Program.ConnectionStrings.Shares); @@ -85,7 +77,6 @@ namespace slskd.Shares /// </summary> public IStateMonitor<SharedFileCacheState> StateMonitor => State; - private StorageMode StorageMode { get; } private int WorkerCount { get; } private ILogger Log { get; } = Serilog.Log.ForContext<ShareScanner>(); private List<Share> Shares { get; set; } @@ -369,58 +360,5 @@ namespace slskd.Shares return false; } - - /// <summary> - /// Attempts to load the cache from disk. - /// </summary> - /// <returns>A value indicating whether the load was successful.</returns> - public bool TryLoad() - { - try - { - // see if we need to 'restore' the database from disk, and do so - if (StorageMode == StorageMode.Memory || (StorageMode == StorageMode.Disk && !Repository.TryValidateTables(Program.ConnectionStrings.Shares))) - { - Log.Debug($"Share cache {(StorageMode == StorageMode.Memory ? "StorageMode is 'Memory'" : "database is missing from disk")}. Attempting to load from backup..."); - - // the backup is missing; we can't do anything but recreate it from scratch - if (!Repository.TryValidateTables(Program.ConnectionStrings.SharesBackup)) - { - Log.Debug("Share cache backup is missing; unable to restore"); - return false; - } - - Log.Debug("Share cache backup located. Attempting to restore..."); - - Sqlite.Restore( - sourceConnectionString: Program.ConnectionStrings.SharesBackup, - destinationConnectionString: Program.ConnectionStrings.Shares); - - Log.Debug("Share cache successfully restored from backup"); - } - - // one of several thigns happened above before we got here: - // the storage mode is memory, and we loaded the in-memory db from a valid backup - // the storage mode is disk, and the file is there and valid - // the storage mode is disk but either missing or invalid, and we restored from a valid backup - // at this point there is a valid (existing, schema matching expected schema) database at the primary connection string - State.SetValue(state => state with - { - Filling = false, - Faulted = false, - Filled = true, - FillProgress = 1, - Directories = Repository.CountDirectories(), - Files = Repository.CountFiles(), - }); - - return true; - } - catch (Exception ex) - { - Log.Debug(ex, "Error loading shared file cache: {Message}", ex.Message); - return false; - } - } } }
\ No newline at end of file diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index decafaa3..d5fd417e 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -20,12 +20,14 @@ using Microsoft.Extensions.Options; namespace slskd.Shares { + using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; + using Serilog; using Soulseek; /// <summary> @@ -42,8 +44,9 @@ namespace slskd.Shares IOptionsMonitor<Options> optionsMonitor, IShareScanner scanner = null) { + CacheStorageMode = optionsMonitor.CurrentValue.Shares.Cache.StorageMode.ToEnum<StorageMode>(); + Scanner = scanner ?? new ShareScanner( - storageMode: optionsMonitor.CurrentValue.Shares.Cache.StorageMode.ToEnum<StorageMode>(), workerCount: optionsMonitor.CurrentValue.Shares.Cache.Workers); Scanner.StateMonitor.OnChange(cacheState => @@ -69,6 +72,8 @@ namespace slskd.Shares OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); + StateMonitor = State; + Configure(OptionsMonitor.CurrentValue); } @@ -80,7 +85,7 @@ namespace slskd.Shares /// <summary> /// Gets the state monitor for the service. /// </summary> - public IStateMonitor<ShareState> StateMonitor => State; + public IStateMonitor<ShareState> StateMonitor { get; } private IShareScanner Scanner { get; } private IShareRepository Repository { get; } @@ -90,6 +95,7 @@ namespace slskd.Shares private IManagedState<ShareState> State { get; } = new ManagedState<ShareState>(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); private List<Regex> FilterRegexes { get; set; } = new List<Regex>(); + private StorageMode CacheStorageMode { get; } /// <summary> /// Returns the entire contents of the share. @@ -241,9 +247,53 @@ namespace slskd.Shares /// Attempt to load shares from disk. /// </summary> /// <returns>A value indicating whether shares were loaded.</returns> - public Task<bool> TryLoadFromDiskAsync() + public bool TryLoadFromDisk() { - return Task.FromResult(Scanner.TryLoad()); + try + { + // see if we need to 'restore' the database from disk, and do so + if (CacheStorageMode == StorageMode.Memory || (CacheStorageMode == StorageMode.Disk && !Repository.TryValidateTables(Program.ConnectionStrings.Shares))) + { + Log.Debug($"Share cache {(CacheStorageMode == StorageMode.Memory ? "StorageMode is 'Memory'" : "database is missing from disk")}. Attempting to load from backup..."); + + // the backup is missing; we can't do anything but recreate it from scratch + if (!Repository.TryValidateTables(Program.ConnectionStrings.SharesBackup)) + { + Log.Debug("Share cache backup is missing; unable to restore"); + return false; + } + + Log.Debug("Share cache backup located. Attempting to restore..."); + + Sqlite.Restore( + sourceConnectionString: Program.ConnectionStrings.SharesBackup, + destinationConnectionString: Program.ConnectionStrings.Shares); + + Log.Debug("Share cache successfully restored from backup"); + } + + // one of several thigns happened above before we got here: + // the storage mode is memory, and we loaded the in-memory db from a valid backup + // the storage mode is disk, and the file is there and valid + // the storage mode is disk but either missing or invalid, and we restored from a valid backup + // at this point there is a valid (existing, schema matching expected schema) database at the primary connection string + State.SetValue(state => state with + { + Scanning = false, + Faulted = false, + Ready = true, + ScanProgress = 1, + Directories = Repository.CountDirectories(), + Files = Repository.CountFiles(), + }); + + return true; + } + catch (Exception ex) + { + Log.Debug(ex, "Error loading shared file cache: {Message}", ex.Message); + return false; + } } private void Configure(Options options) |
