From bf3e8790af148e5c9cb48ebc16308ce926dba8f5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Sep 2022 10:48:48 -0500 Subject: remove loading from scanner --- src/slskd/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Application.cs') 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."); -- cgit v1.2.3 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/slskd/Application.cs') 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 From 664a3e988a0f0beb2f5a75602ea468d339ac87a6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 1 Oct 2022 14:59:30 -0500 Subject: force a rescan if option is set --- src/slskd/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index da0688dd..2a52f2f5 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -375,7 +375,7 @@ namespace slskd // if this is the first run or the user deleted cached files, a full scan will be performed if (!OptionsAtStartup.Flags.NoShareScan) { - await Shares.InitializeAsync(); + await Shares.InitializeAsync(forceRescan: OptionsAtStartup.Flags.ForceShareScan); } if (OptionsAtStartup.Flags.NoConnect) -- cgit v1.2.3 From 36b1e1d4537d817d7a02987a25e5f0cc792d5f8f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 1 Oct 2022 15:05:43 -0500 Subject: respect force scan option, gracefully handle init failures --- src/slskd/Application.cs | 11 ++++++++--- src/slskd/Shares/ShareService.cs | 12 ++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 2a52f2f5..57f968ec 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -371,11 +371,16 @@ namespace slskd _ = CheckVersionAsync(); } - // 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) { - await Shares.InitializeAsync(forceRescan: OptionsAtStartup.Flags.ForceShareScan); + try + { + await Shares.InitializeAsync(forceRescan: OptionsAtStartup.Flags.ForceShareScan); + } + catch (Exception) + { + Log.Error("Failed to initialize shares. Sharing is disabled"); + } } if (OptionsAtStartup.Flags.NoConnect) diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 86b19f3d..cebb15f6 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -252,8 +252,12 @@ namespace slskd.Shares { try { - // see if we need to 'restore' the database from disk, and do so - if (CacheStorageMode == StorageMode.Memory || (CacheStorageMode == StorageMode.Disk && !ShareRepository.TryValidateDatabase(Program.ConnectionStrings.Shares))) + if (forceRescan) + { + Log.Warning("Performing a forced re-scan of shares"); + await ScanAsync(); + } + else if (CacheStorageMode == StorageMode.Memory || (CacheStorageMode == StorageMode.Disk && !ShareRepository.TryValidateDatabase(Program.ConnectionStrings.Shares))) { Log.Debug($"Share cache {(CacheStorageMode == StorageMode.Memory ? "StorageMode is 'Memory'" : "database is missing from disk")}. Attempting to load from backup..."); @@ -261,7 +265,7 @@ namespace slskd.Shares if (!ShareRepository.TryValidateDatabase(Program.ConnectionStrings.SharesBackup)) { Log.Warning("Share cache couldn't be loaded from backup"); - Log.Warning("Performing a full share scan to rebuild the cache"); + await InitializeAsync(forceRescan: true); } Log.Debug("Share cache backup located. Attempting to restore..."); @@ -298,7 +302,7 @@ namespace slskd.Shares } else { - Log.Error("Failed to initialize shares, and an attempt to force a full scan to repair failed. Check your filesystem."); + Log.Error("Failed to initialize shares, and an attempt to force a full scan to repair failed"); throw; } } -- cgit v1.2.3