From 5c51df5b8fb8e03f57bf0fbef70f37d5def52d7d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Jan 2024 15:54:35 -0600 Subject: add a flag to suppress config file watching --- config/slskd.example.yml | 1 + src/slskd/Core/Options.cs | 9 +++++++++ src/slskd/Program.cs | 8 ++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index b1f4ce08..8a4ed133 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -5,6 +5,7 @@ # flags: # no_logo: false # no_start: false +# no_config_watch: false # no_connect: false # no_share_scan: false # force_share_scan: false diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index c1f4d558..3727b89d 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -332,6 +332,15 @@ namespace slskd [RequiresRestart] public bool NoStart { get; init; } = false; + /// + /// Gets a value indicating whether the application should watch the configuration file for changes. + /// + [Argument(default, "no-config-watch")] + [EnvironmentVariable("NO_CONFIG_WATCH")] + [Description("do not watch the config file for changes")] + [RequiresRestart] + public bool NoConfigWatch { get; init; } = false; + /// /// Gets a value indicating whether the application should connect to the Soulseek network on startup. /// diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 03cc6c75..8aa1e102 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -350,7 +350,7 @@ namespace slskd try { Configuration = new ConfigurationBuilder() - .AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile) + .AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile, reloadOnChange: !OptionsAtStartup.Flags.NoConfigWatch) .Build(); Configuration.GetSection(AppName) @@ -436,7 +436,7 @@ namespace slskd var builder = WebApplication.CreateBuilder(args); builder.Configuration - .AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile); + .AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile, reloadOnChange: !OptionsAtStartup.Flags.NoConfigWatch); builder.Host .UseSerilog(); @@ -1019,7 +1019,7 @@ namespace slskd .CreateLogger(); } - private static IConfigurationBuilder AddConfigurationProviders(this IConfigurationBuilder builder, string environmentVariablePrefix, string configurationFile) + private static IConfigurationBuilder AddConfigurationProviders(this IConfigurationBuilder builder, string environmentVariablePrefix, string configurationFile, bool reloadOnChange) { configurationFile = Path.GetFullPath(configurationFile); @@ -1045,7 +1045,7 @@ namespace slskd path: Path.GetFileName(configurationFile), targetType: typeof(Options), optional: true, - reloadOnChange: true, + reloadOnChange: reloadOnChange, provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) // required for locations outside of the app directory .AddCommandLine( targetType: typeof(Options), -- cgit v1.2.3 From b448b2c6e2999ea28bc0a4f1c177d041f2ec265c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Jan 2024 16:03:20 -0600 Subject: add NoConfigWatch docs, fix missing flags --- config/slskd.example.yml | 1 + docs/config.md | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 8a4ed133..eb0ffce0 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -13,6 +13,7 @@ # log_sql: false # experimental: false # volatile: false +# case_sensitive_reg_ex: false # relay: # enabled: false # mode: controller # controller (default), agent, or debug (for local development) diff --git a/docs/config.md b/docs/config.md index a7206d60..15a4c817 100644 --- a/docs/config.md +++ b/docs/config.md @@ -963,6 +963,7 @@ Several additional feature flags are provided to change the application's runtim | `--experimental` | `SLSKD_EXPERIMENTAL` | Run the application in experimental mode. YMMV. | | `-n\|--no-logo` | `SLSKD_NO_LOGO` | Don't show the application logo on startup | | `-x\|--no-start` | `SLSKD_NO_START` | Bootstrap the application, but don't start | +| `--no-config-watch` | `SLSKD_NO_CONFIG_WATCH` | Don't watch the configuration file for changes | | `--no-connect` | `SLSKD_NO_CONNECT` | Start the application, but don't connect to the server | | `--no-share-scan` | `SLSKD_NO_SHARE_SCAN` | Don't perform a scan of shared directories on startup | | `--force-share-scan` | `SLSKD_FORCE_SHARE_SCAN` | Force a scan of shared directories on startup | @@ -977,12 +978,15 @@ debug: false flags: no_logo: false no_start: false + no_config_watch: false no_connect: false no_share_scan: false + force_share_scan: false no_version_check: false log_sql: false + experimental: false volatile: false - case_sensitive_regex: false + case_sensitive_reg_ex: false ``` # Commands -- cgit v1.2.3 From 60e58c737e2f4dc7bc59e420f1bab98bf27f5583 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Jan 2024 16:28:28 -0600 Subject: log warning when config watch is disabled --- src/slskd/Program.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 8aa1e102..ad50dccd 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -412,6 +412,12 @@ namespace slskd Log.Information("Using application directory {AppDirectory}", AppDirectory); Log.Information("Using configuration file {ConfigurationFile}", ConfigurationFile); + + if (OptionsAtStartup.Flags.NoConfigWatch) + { + Log.Warning("Configuration watch DISABLED; all configuration changes will require a restart to take effect"); + } + Log.Information("Storing application data in {DataDirectory}", DataDirectory); if (OptionsAtStartup.Logger.Disk) -- cgit v1.2.3 From 4fd9cb4d9097f242b01198a4e09f2b425e219861 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Jan 2024 16:29:14 -0600 Subject: raise pendingrestart flag if config is changed while watch is disabled --- src/slskd/Core/API/Controllers/OptionsController.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 622a0273..aeeafd2b 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -42,14 +42,17 @@ namespace slskd.Core.API { public OptionsController( OptionsAtStartup optionsAtStartup, - IOptionsSnapshot optionsSnapshot) + IOptionsSnapshot optionsSnapshot, + IStateMutator stateMutator) { OptionsAtStartup = optionsAtStartup; OptionsSnapshot = optionsSnapshot; + StateMutator = stateMutator; } private IOptionsSnapshot OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } + private IStateMutator StateMutator { get; } private ILogger Logger { get; } = Log.ForContext(typeof(OptionsController)); /// @@ -146,6 +149,13 @@ namespace slskd.Core.API try { IOFile.WriteAllText(Program.ConfigurationFile, yaml); + + if (OptionsSnapshot.Value.Flags.NoConfigWatch) + { + Logger.Information("Configuration watch is disabled; restart required for changes to take effect"); + StateMutator.SetValue(state => state with { PendingRestart = true }); + } + return Ok(); } catch (Exception ex) -- cgit v1.2.3 From e4d81d4d208e6ee5bc9a44b74dab4eaf146a819d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Jan 2024 16:29:43 -0600 Subject: log exception when validation fails --- src/slskd/Core/API/Controllers/OptionsController.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index aeeafd2b..e39c5fe1 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -198,6 +198,8 @@ namespace slskd.Core.API } catch (Exception ex) { + Logger.Warning(ex, "Configuration validation failed"); + error = ex.Message; if (ex.InnerException != null) -- cgit v1.2.3