Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Host] Allow disabling reloadOnChange for Host's CreateDefaultBuilder #2940

Merged
6 commits merged into from
Mar 19, 2020
Merged

[Host] Allow disabling reloadOnChange for Host's CreateDefaultBuilder #2940

6 commits merged into from
Mar 19, 2020

Conversation

kjerk
Copy link

@kjerk kjerk commented Feb 5, 2020

  • Add ASPNETCORE_BUILDER_CONFIG_RELOAD environment flag to allow disabling live reload in builder.
  • Add two tests to assert two different cases (live reload on, live reload off) work.

Addresses #2939

src/Hosting/Hosting/src/Host.cs Outdated Show resolved Hide resolved
src/Hosting/Hosting/test/HostTests.cs Outdated Show resolved Hide resolved
src/Hosting/Hosting/test/HostTests.cs Outdated Show resolved Hide resolved
src/Hosting/Hosting/test/HostTests.cs Outdated Show resolved Hide resolved
@@ -70,9 +70,11 @@ public static IHostBuilder CreateDefaultBuilder(string[] args)
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;

var reloadOnChange = hostingContext.Configuration.GetValue("HOSTBUILDER_CONFIG_RELOAD", true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The host uses top level keys like contentRoot.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1#contentrootpath
configReload should be adequate here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some prefix I can add to that to disambiguate? I was trying to follow the {ASPNETCORE/DOTNET}_FEATURE_DESCRIPTION nomenclature of other aspnetcore and dotnet flags sort of like DOTNET_USE_POLLING_FILE_WATCHER

This would mean that DOTNET_CONFIGRELOAD specified in an environment variable would trip the behavior added in this PR, and that to me sounds like it's entirely disabling config reloading, which is not quite the case, as one's own calls to AddJsonFile are left alone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can at least use the hierarchical config pattern.
Also, config keys are not case sensitive. Changed for readability.

Suggested change
var reloadOnChange = hostingContext.Configuration.GetValue("HOSTBUILDER_CONFIG_RELOAD", true);
var reloadFlagConfig = new Dictionary<string, string>() {{ "hostbuilder:configreload", "false" }};

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, this has been updated in my most recent commit de16b88

@@ -70,9 +70,11 @@ public static IHostBuilder CreateDefaultBuilder(string[] args)
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;

var reloadOnChange = hostingContext.Configuration.GetValue("HOSTBUILDER_CONFIG_RELOAD", true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can at least use the hierarchical config pattern.
Also, config keys are not case sensitive. Changed for readability.

Suggested change
var reloadOnChange = hostingContext.Configuration.GetValue("HOSTBUILDER_CONFIG_RELOAD", true);
var reloadFlagConfig = new Dictionary<string, string>() {{ "hostbuilder:configreload", "false" }};

src/Hosting/Hosting/test/HostTests.cs Outdated Show resolved Hide resolved
src/Hosting/Hosting/test/HostTests.cs Outdated Show resolved Hide resolved
@Tratcher Tratcher self-assigned this Feb 14, 2020
@kjerk
Copy link
Author

kjerk commented Mar 4, 2020

Sorry it took a while to get back around to this one, and thanks for the feedback. I pushed de16b88 to resolve (I think) any dangling parts from the conversations.

Changelist

  • Update config key to hostbuilder:configreload
  • Changed both tests to async Task instead of being non async methods.
  • For the positive test case, increase wait time on test, listen for config.GetReloadToken() to fire and if it does, cancel any waiting and proceed.
  • Cancelling a task throws, so rather than write a full try/catch for the whole thing, wrap Task.Delay() in Task.WhenAny() which will ignore the throw and function as normal to keep it terse.

Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shirhatti can you sanity check this for usability? CreateDefaultBuilder reads an entry "hostbuilder:configreload" from host config to enable/disable reload for appsettings.json.

src/Hosting/Hosting/src/Host.cs Outdated Show resolved Hide resolved
Copy link

@analogrelay analogrelay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ok to me. I’ll leave it to @Tratcher to approve though.

@ghost
Copy link

ghost commented Mar 19, 2020

Hello @Tratcher!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@Tratcher Tratcher added this to the 5.0.0-preview3 milestone Mar 19, 2020
@ghost ghost merged commit 64140f9 into dotnet:master Mar 19, 2020
@Tratcher
Copy link
Member

Thanks @kjerk

@kjerk kjerk deleted the HostBuilderDefaultReload branch March 19, 2020 19:21
@kjerk
Copy link
Author

kjerk commented Mar 19, 2020

Thanks @kjerk

Thanks everybody.
Seems like a small change but this helps our prod deployments in Centos a lot.

maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 27, 2020
…dotnet/extensions#2940)

* Add environment var to allow disabling live reload in default builder

* Switch to using hostingContext, rename flag to fit nomenclature.

* Change flag to memory data source. Change content root default instead of runtime default.

* Update config key to be hierarchical. Change await on positive case to be longer and cancel using the reload token.

* Uncomment a development test case.

* Apply suggestions from code review

Co-authored-by: Chris Ross <Tratcher@Outlook.com>


Commit migrated from dotnet/extensions@64140f9
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 27, 2020
…dotnet/extensions#2940)

* Add environment var to allow disabling live reload in default builder

* Switch to using hostingContext, rename flag to fit nomenclature.

* Change flag to memory data source. Change content root default instead of runtime default.

* Update config key to be hierarchical. Change await on positive case to be longer and cancel using the reload token.

* Uncomment a development test case.

* Apply suggestions from code review

Co-authored-by: Chris Ross <Tratcher@Outlook.com>


Commit migrated from dotnet/extensions@64140f9
@davidfowl
Copy link
Member

The name of this configuration entry isn't ideal. Maybe it should be host and not hostbuilder?

@@ -70,9 +70,11 @@ public static IHostBuilder CreateDefaultBuilder(string[] args)
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;

var reloadOnChange = hostingContext.Configuration.GetValue("hostBuilder:reloadConfigOnChange", defaultValue: true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also be set to https://github.com/dotnet/runtime/blob/79ae74f5ca5c8a6fe3a48935e85bd7374959c570/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationSource.cs#L33?

Apparently this condition is true: https://github.com/dotnet/runtime/blob/79ae74f5ca5c8a6fe3a48935e85bd7374959c570/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs#L34 even with "hostBuilder": {"reloadConfigOnChange": false } in appsettings.json.

kernel implementation without inotify support throw the following exception on application startup and there seems to be no way to turn (this quite an unimportant) feature off:

Unhandled exception. System.IO.IOException: Function not implemented
   at System.IO.FileSystemWatcher.StartRaisingEvents()
   at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
   at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher()
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
   at Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(String filter)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.<.ctor>b__1_0()
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource source)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at mymvc1.Program.Main(String[] args)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got the global opt-out knob working with: dotnet/runtime@457ed11; DOTNET_USE_POLLING_FILE_WATCHER=1 in unikernel environment completely disables inotify. sadly, it wasn't backported to net5 so i switched to net6..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for HostBuilder, at the time this was being changed if you used CreateDefaultBuilder() you were getting live reloading whether you wanted it or not because it was hard coded to true

If Source.ReloadOnChange is populated by an env var and is available by the time ConfigureAppConfiguration() fires then that would work a bit better.

@davidfowl
Copy link
Member

What did we merge here? Did we change the key to host vs hostBuilder?

@kjerk
Copy link
Author

kjerk commented Mar 8, 2021

Looking back at this it looks like the config change was mentioned a few months after it was merged/closed (Mar 19).

I think iirc at the time when referencing the docs, while Host.blah was used as a static factory method, most things were referencing IHostBuilder, CreateHostBuilder, CreateDefaultBuilder, etc etc, and the only other ENV var override I could find referenced the implementation class, so I just defaulted to hostBuilder, though host is a bit nicer and agnostic sounding.

To make the change would require subsequent changes and maybe another PR?

Also because I myself had trouble finding the tiny link at the top of this PR, the original issue this is resolving is here: #2939 which gives some details on what problem this was addressing.

@drdamour
Copy link

was ASPNETCORE_BUILDER_CONFIG_RELOAD used, i can't find any reference to it through google, or was this solved some totally different way?

@Tratcher
Copy link
Member

https://github.com/dotnet/runtime/blob/02ad981eab46339b6abef5b97b3db761d22b6455/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L237
It would be DOTNET_hostBuilder_reloadConfigOnChange=true

@ghost ghost locked as resolved and limited conversation to collaborators May 26, 2023
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants