From efb613beb826bd27b8296c75d61993bdbdcc0bb5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 1 Oct 2022 14:58:04 -0500 Subject: move init to ShareService --- src/slskd/Application.cs | 19 ++++--------------- src/slskd/Shares/IShareService.cs | 7 ++++--- src/slskd/Shares/ShareService.cs | 30 ++++++++++++++++++++---------- 3 files changed, 28 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 7e9ad4fd..da0688dd 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -371,22 +371,11 @@ namespace slskd _ = CheckVersionAsync(); } - // 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 (!Shares.TryLoadFromDisk()) + // initialize shares. most of the time shares will be loaded from disk, but + // if this is the first run or the user deleted cached files, a full scan will be performed + if (!OptionsAtStartup.Flags.NoShareScan) { - Log.Warning("Unable to load share cache from disk. A share scan is required."); - - State.SetValue(state => state with { Shares = state.Shares with { ScanPending = true } }); - - if (OptionsAtStartup.Flags.NoShareScan) - { - Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); - } - else - { - await Shares.ScanAsync(); - } + await Shares.InitializeAsync(); } if (OptionsAtStartup.Flags.NoConnect) diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index 429ab529..2a18f5b3 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -90,9 +90,10 @@ namespace slskd.Shares bool TryCancelScan(); /// - /// Attempt to load shares from disk. + /// Initializes the service and shares. /// - /// A value indicating whether shares were loaded. - bool TryLoadFromDisk(); + /// A value indicating whether a full re-scan of shares should be performed. + /// The operation context. + Task InitializeAsync(bool forceRescan = false); } } \ No newline at end of file diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index ff40481e..86b19f3d 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -244,10 +244,11 @@ namespace slskd.Shares } /// - /// Attempt to load shares from disk. + /// Initializes the service and shares. /// - /// A value indicating whether shares were loaded. - public bool TryLoadFromDisk() + /// A value indicating whether a full re-scan of shares should be performed. + /// The operation context. + public async Task InitializeAsync(bool forceRescan = false) { try { @@ -259,21 +260,22 @@ namespace slskd.Shares // the backup is missing; we can't do anything but recreate it from scratch if (!ShareRepository.TryValidateDatabase(Program.ConnectionStrings.SharesBackup)) { - Log.Debug("Share cache backup is missing; unable to restore"); - return false; + Log.Warning("Share cache couldn't be loaded from backup"); + Log.Warning("Performing a full share scan to rebuild the cache"); } Log.Debug("Share cache backup located. Attempting to restore..."); Repository.RestoreFrom(connectionString: Program.ConnectionStrings.SharesBackup); - Log.Debug("Share cache successfully restored from backup"); + Log.Information("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 + // we failed to load the cache from disk or backup, and performed a full scan successfully // at this point there is a valid (existing, schema matching expected schema) database at the primary connection string State.SetValue(state => state with { @@ -284,13 +286,21 @@ namespace slskd.Shares Directories = Repository.CountDirectories(), Files = Repository.CountFiles(), }); - - return true; } catch (Exception ex) { - Log.Debug(ex, "Error loading shared file cache: {Message}", ex.Message); - return false; + Log.Warning(ex, "Error initializing shares: {Message}", ex.Message); + + if (!forceRescan) + { + Log.Warning("Re-attempting initializtion"); + await InitializeAsync(forceRescan: true); + } + else + { + Log.Error("Failed to initialize shares, and an attempt to force a full scan to repair failed. Check your filesystem."); + throw; + } } } -- cgit v1.2.3