diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-01-21 16:32:33 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-21 16:32:33 -0600 |
| commit | 33d8694d7ed6ee7e0828d2af1fa0d0248c14bec2 (patch) | |
| tree | 65a392669ebdf4a4e62cfc7a2a810aaa72cabd89 | |
| parent | 416d0451118d69384402100ef24d63af7e1835b4 (diff) | |
| parent | e4d81d4d208e6ee5bc9a44b74dab4eaf146a819d (diff) | |
| download | slskd-33d8694d7ed6ee7e0828d2af1fa0d0248c14bec2.tar.gz | |
Merge pull request #1058 from slskd/watch
Add 'NoConfigWatch' flag to suppress config file watching
| -rw-r--r-- | config/slskd.example.yml | 2 | ||||
| -rw-r--r-- | docs/config.md | 6 | ||||
| -rw-r--r-- | src/slskd/Core/API/Controllers/OptionsController.cs | 14 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 14 |
5 files changed, 39 insertions, 6 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index b1f4ce08..eb0ffce0 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 @@ -12,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 diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 622a0273..e39c5fe1 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<Options> optionsSnapshot) + IOptionsSnapshot<Options> optionsSnapshot, + IStateMutator<State> stateMutator) { OptionsAtStartup = optionsAtStartup; OptionsSnapshot = optionsSnapshot; + StateMutator = stateMutator; } private IOptionsSnapshot<Options> OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } + private IStateMutator<State> StateMutator { get; } private ILogger Logger { get; } = Log.ForContext(typeof(OptionsController)); /// <summary> @@ -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) @@ -188,6 +198,8 @@ namespace slskd.Core.API } catch (Exception ex) { + Logger.Warning(ex, "Configuration validation failed"); + error = ex.Message; if (ex.InnerException != null) 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 @@ -333,6 +333,15 @@ namespace slskd public bool NoStart { get; init; } = false; /// <summary> + /// Gets a value indicating whether the application should watch the configuration file for changes. + /// </summary> + [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; + + /// <summary> /// Gets a value indicating whether the application should connect to the Soulseek network on startup. /// </summary> [Argument(default, "no-connect")] diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 03cc6c75..ad50dccd 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) @@ -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) @@ -436,7 +442,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 +1025,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 +1051,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), |
