From 1cc28437711fdca0643f01191f84325e3b2359cb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Sep 2022 17:41:06 -0500 Subject: hard code filenames, make SQLite init more prominent --- src/slskd/Program.cs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index f4e59456..5a5d9ce9 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -252,17 +252,31 @@ namespace slskd return; } + InitSQLiteOrFailFast(); + // the application isn't being run in command mode. derive the application directory value // and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; - DataDirectory = Path.Combine(AppDirectory, "data"); + // configure connection strings and configure SQLite + string shareDbDataSource = default; + + if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) + { + shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); + } + else + { + shareDbDataSource = "file:shares?mode=memory"; + } + ConnectionStrings = new() { - Search = $"Data Source={Path.Combine(DataDirectory, Filenames.SearchDb)};Cache=shared;Pooling=True;", - Transfers = $"Data Source={Path.Combine(DataDirectory, Filenames.TransfersDb)};Cache=shared;Pooling=True;", - Shares = $"Data Source={Path.Combine(DataDirectory, Filenames.SharesDb)};Cache=shared", + Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", + Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", + Shares = $"Data Source={shareDbDataSource};Cache=shared", + SharesBackup = $"Data Source={Path.Combine(DataDirectory, "shares.db.bak")};", }; DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); @@ -465,8 +479,6 @@ namespace slskd services.AddSingleton(); - ConfigureSQLite(); - services.AddDbContext(ConnectionStrings.Search); services.AddDbContext(ConnectionStrings.Transfers); @@ -492,7 +504,7 @@ namespace slskd return services; } - private static void ConfigureSQLite() + private static void InitSQLiteOrFailFast() { // initialize // avoids: System.Exception: You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init(). -- cgit v1.2.3 From 2a0db7e44a0b25d2a505ae8472de214c6c68b5d2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 Sep 2022 12:02:17 -0500 Subject: move connection string setup --- src/slskd/Program.cs | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 5a5d9ce9..fe2bb626 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -252,33 +252,11 @@ namespace slskd return; } - InitSQLiteOrFailFast(); - // the application isn't being run in command mode. derive the application directory value // and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; DataDirectory = Path.Combine(AppDirectory, "data"); - // configure connection strings and configure SQLite - string shareDbDataSource = default; - - if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) - { - shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); - } - else - { - shareDbDataSource = "file:shares?mode=memory"; - } - - ConnectionStrings = new() - { - Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", - Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", - Shares = $"Data Source={shareDbDataSource};Cache=shared", - SharesBackup = $"Data Source={Path.Combine(DataDirectory, "shares.db.bak")};", - }; - DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); DefaultDownloadsDirectory = Path.Combine(AppDirectory, "downloads"); DefaultIncompleteDirectory = Path.Combine(AppDirectory, "incomplete"); @@ -355,11 +333,38 @@ namespace slskd Log.Information("Process ID: {ProcessId}", ProcessId); Log.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName); + // SQLite must have specific capabilities to function properly. this shouldn't be a concern for shrinkwrapped + // binaries or in Docker, but if someone builds from source weird things can happen. + InitSQLiteOrFailFast(); + Log.Information("Using application directory {AppDirectory}", AppDirectory); Log.Information("Using configuration file {ConfigurationFile}", ConfigurationFile); + Log.Information("Storing application data in {DataDirectory}", DataDirectory); RecreateConfigurationFileIfMissing(ConfigurationFile); + // configure connection strings and configure SQLite + string shareDbDataSource = default; + + if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) + { + Log.Information("Using on-disk shared file database"); + shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); + } + else + { + Log.Information("Using in-memory shared file database"); + shareDbDataSource = "file:shares?mode=memory"; + } + + ConnectionStrings = new() + { + Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", + Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", + Shares = $"Data Source={shareDbDataSource};Cache=shared", + SharesBackup = $"Data Source={Path.Combine(DataDirectory, "shares.db.bak")};", + }; + if (!string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki)) { Log.Information("Forwarding logs to Grafana Loki instance at {LoggerLokiUrl}", OptionsAtStartup.Logger.Loki); -- cgit v1.2.3 From a596dfe252ef53e41b91ec6773aec93307c41f1e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 Sep 2022 18:09:58 -0500 Subject: tweak logging for consistency --- src/slskd/Application.cs | 2 +- src/slskd/Program.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 288ba07b..cab2b649 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1032,7 +1032,7 @@ namespace slskd { // the share transitioned into ready without completing a scan; it was loaded from disk State.SetValue(state => state with { Shares = state.Shares with { ScanPending = false } }); - Log.Information("Shares loaded from disk successfully. Sharing {Directories} directories and {Files} files", current.Directories, current.Files); + Log.Information("Share cache loaded from disk successfully. Sharing {Directories} directories and {Files} files", current.Directories, current.Files); _ = CacheBrowseResponse(); } else diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index fe2bb626..e828f242 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -348,12 +348,12 @@ namespace slskd if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) { - Log.Information("Using on-disk shared file database"); + Log.Information("Using on-disk shared file cache"); shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); } else { - Log.Information("Using in-memory shared file database"); + Log.Information("Using in-memory shared file cache"); shareDbDataSource = "file:shares?mode=memory"; } -- cgit v1.2.3