Skip to content

Q: Is it possible to trigger a drainstop & restart on configuration changes #10620

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

Closed
grahamehorner opened this issue May 29, 2019 · 21 comments
Closed
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Milestone

Comments

@grahamehorner
Copy link
Contributor

I'm looking for a way to drainstop a kestrel web host and restart the service via a configuration change; is this possible ? how would one inform the kestrel process to stop accepting request without impacting existing requests in process/queue?

@analogrelay
Copy link
Contributor

analogrelay commented May 29, 2019

We don't have anything built in to wire these up. You can use the ReloadToken property on IConfiguration to get signalled whenever configuration changes and shut the server down yourself though.

I believe you can use the StopApplication (which you can inject from DI) method on IApplicationLifetime to trigger a "graceful" shutdown (where existing requests continue to be processed).
@Tratcher @halter73 am I correct here?

As for restarting, there's nothing built-in to trigger a restart. You have a few options here. You could write code in your Program.Main to automatically restart the server if the shutdown is due to a configuration reload (you'll have to flow this information from the config reload callback yourself). Or you could have a "watchdog" process running that auto-restarts the server when it shuts down.

@analogrelay analogrelay added this to the Discussions milestone May 29, 2019
@Tratcher
Copy link
Member

Correct, with a default 5 second graceful shutdown timeout.

That said, config has change detection and some components respond to it. What are you re-configuring?

@halter73
Copy link
Member

If you do still need to restart the app to react to config changes, and the default 5 second graceful shutdown timeout is insufficient, you can configure it. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.2#shutdown-timeout

@grahamehorner
Copy link
Contributor Author

@anurse @Tratcher @halter73 thanks for the information; in regards to stopping/pausing the acceptance of new request and/or checking the request/response queue so you may prevent termination of the requests in process ?

@Tratcher
Copy link
Member

I don't understand how that's different from your original question.

@grahamehorner
Copy link
Contributor Author

@Tratcher the usecase/flow here is:-

  1. a change in configuration is detected
  2. inform kestrel not to accept anymore requests
  3. check how many request or in process
  4. wait until request in process complete
  5. restart kestrel

my follow-up question is how can I hook into kestrel to identify how many requests are in-process or have been queued? and/or how long a request as been in-process/queued?

I'm not sure of what the shutdown timeout would be? as all queued request must be processed prior to shutdown/restart.

@grahamehorner
Copy link
Contributor Author

@Tratcher with regards to what is getting re-configured this could be anything from digital certificates to database connection string and anything in-between and therefore we opted for a process where a configuration change from a given source would restart the main webhost task.

@Tratcher
Copy link
Member

inform kestrel not to accept anymore requests

StopApplication handles this.

check how many request or in process

That's not something you need to worry about, Kestrel takes care of it.

wait until request in process complete

Kestrel will do this, but it will abort requests after a given timeout (5 seconds by default). You can raise that timeout as needed, but I don't suggest disabling it. You wouldn't want a slow request to prevent your site from restarting indefinitely.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.2#shutdown-timeout

@grahamehorner
Copy link
Contributor Author

@Tratcher hmmm, we don't what a restart to terminate any request; if a request is stuck or slow to complete, then manual intervention is required or we need some way of capturing these prior to killing the request and restarting. Q: is there any logging with regard to kestrel when a StopApplication is received ie. the status of the request queue time shutdown received etc. ?

@Tratcher
Copy link
Member

At this point I suggest trying StopApplication to see how well it works for you.

@grahamehorner
Copy link
Contributor Author

@Tratcher Yes, thanks we are looking at this at present and will update soon with regards to our usecase

@grahamehorner
Copy link
Contributor Author

@Tratcher I've got a simple application listening for configuration changes and calling the StopApplication as discussed, however the configuration change event is fired many times on a single simple change? which seems odd? are you aware of any issues around IChangeToken events ?

simple log with a count outputs :=

WebApplicationWithRestart.Startup:Information: configuration change detected 1
WebApplicationWithRestart.Startup:Information: application stopping.
WebApplicationWithRestart.Startup:Information: configuration change detected 2
WebApplicationWithRestart.Startup:Information: configuration change detected 3
WebApplicationWithRestart.Startup:Information: configuration change detected 4
WebApplicationWithRestart.Startup:Information: configuration change detected 5
WebApplicationWithRestart.Startup:Information: configuration change detected 6
WebApplicationWithRestart.Startup:Information: configuration change detected 7
WebApplicationWithRestart.Startup:Information: configuration change detected 8
WebApplicationWithRestart.Startup:Information: configuration change detected 9
WebApplicationWithRestart.Startup:Information: configuration change detected 10
WebApplicationWithRestart.Startup:Information: configuration change detected 11
WebApplicationWithRestart.Startup:Information: configuration change detected 12
WebApplicationWithRestart.Startup:Information: configuration change detected 13
WebApplicationWithRestart.Startup:Information: configuration change detected 14
WebApplicationWithRestart.Startup:Information: configuration change detected 15
WebApplicationWithRestart.Startup:Information: configuration change detected 16
WebApplicationWithRestart.Startup:Information: configuration change detected 17
WebApplicationWithRestart.Startup:Information: configuration change detected 18
WebApplicationWithRestart.Startup:Information: configuration change detected 19
WebApplicationWithRestart.Startup:Information: configuration change detected 20
WebApplicationWithRestart.Startup:Information: configuration change detected 21
WebApplicationWithRestart.Startup:Information: configuration change detected 22
WebApplicationWithRestart.Startup:Information: configuration change detected 23
WebApplicationWithRestart.Startup:Information: configuration change detected 24
WebApplicationWithRestart.Startup:Information: configuration change detected 25
WebApplicationWithRestart.Startup:Information: configuration change detected 26
WebApplicationWithRestart.Startup:Information: configuration change detected 27
WebApplicationWithRestart.Startup:Information: configuration change detected 28
WebApplicationWithRestart.Startup:Information: configuration change detected 29
WebApplicationWithRestart.Startup:Information: configuration change detected 30
WebApplicationWithRestart.Startup:Information: configuration change detected 31
WebApplicationWithRestart.Startup:Information: configuration change detected 32
WebApplicationWithRestart.Startup:Information: configuration change detected 33
WebApplicationWithRestart.Startup:Information: configuration change detected 34
WebApplicationWithRestart.Startup:Information: configuration change detected 35
WebApplicationWithRestart.Startup:Information: configuration change detected 36
WebApplicationWithRestart.Startup:Information: configuration change detected 37
WebApplicationWithRestart.Startup:Information: configuration change detected 38
WebApplicationWithRestart.Startup:Information: configuration change detected 39
WebApplicationWithRestart.Startup:Information: configuration change detected 40
WebApplicationWithRestart.Startup:Information: configuration change detected 41
WebApplicationWithRestart.Startup:Information: configuration change detected 42
WebApplicationWithRestart.Startup:Information: configuration change detected 43
WebApplicationWithRestart.Startup:Information: configuration change detected 44
WebApplicationWithRestart.Startup:Information: configuration change detected 45
WebApplicationWithRestart.Startup:Information: configuration change detected 46
WebApplicationWithRestart.Startup:Information: configuration change detected 47
WebApplicationWithRestart.Startup:Information: configuration change detected 48
WebApplicationWithRestart.Startup:Information: configuration change detected 49
WebApplicationWithRestart.Startup:Information: application stopped.
WebApplicationWithRestart.Startup:Information: configuration change detected 50
WebApplicationWithRestart.Startup:Information: configuration change detected 51
WebApplicationWithRestart.Startup:Information: configuration change detected 52
WebApplicationWithRestart.Startup:Information: configuration change detected 53
WebApplicationWithRestart.Startup:Information: configuration change detected 54
WebApplicationWithRestart.Startup:Information: configuration change detected 55
WebApplicationWithRestart.Startup:Information: configuration change detected 56
WebApplicationWithRestart.Startup:Information: configuration change detected 57
WebApplicationWithRestart.Startup:Information: configuration change detected 58
WebApplicationWithRestart.Startup:Information: configuration change detected 59
WebApplicationWithRestart.Startup:Information: configuration change detected 60
WebApplicationWithRestart.Startup:Information: configuration change detected 61
WebApplicationWithRestart.Startup:Information: configuration change detected 62
WebApplicationWithRestart.Startup:Information: configuration change detected 63
WebApplicationWithRestart.Startup:Information: configuration change detected 64
WebApplicationWithRestart.Startup:Information: configuration change detected 65
WebApplicationWithRestart.Startup:Information: configuration change detected 66
WebApplicationWithRestart.Startup:Information: configuration change detected 67
WebApplicationWithRestart.Startup:Information: configuration change detected 68
WebApplicationWithRestart.Startup:Information: configuration change detected 69
WebApplicationWithRestart.Startup:Information: configuration change detected 70
WebApplicationWithRestart.Startup:Information: configuration change detected 71
WebApplicationWithRestart.Startup:Information: configuration change detected 72
WebApplicationWithRestart.Startup:Information: configuration change detected 73
WebApplicationWithRestart.Startup:Information: configuration change detected 74
WebApplicationWithRestart.Startup:Information: configuration change detected 75
WebApplicationWithRestart.Startup:Information: configuration change detected 76
WebApplicationWithRestart.Startup:Information: configuration change detected 77
WebApplicationWithRestart.Startup:Information: configuration change detected 78
WebApplicationWithRestart.Startup:Information: configuration change detected 79
WebApplicationWithRestart.Startup:Information: configuration change detected 80
WebApplicationWithRestart.Startup:Information: configuration change detected 81
WebApplicationWithRestart.Startup:Information: configuration change detected 82
WebApplicationWithRestart.Startup:Information: configuration change detected 83
WebApplicationWithRestart.Startup:Information: configuration change detected 84
WebApplicationWithRestart.Startup:Information: configuration change detected 85
WebApplicationWithRestart.Startup:Information: configuration change detected 86
WebApplicationWithRestart.Startup:Information: configuration change detected 87
WebApplicationWithRestart.Startup:Information: configuration change detected 88

@Tratcher
Copy link
Member

That's a lot of changes 😁. What config source actually changed? A file?

@HaoK ?

@grahamehorner
Copy link
Contributor Author

@Tratcher I changed the appsettings.json logging level from within VS 2019 when running the code in debug

@HaoK
Copy link
Member

HaoK commented May 31, 2019

Yeah the config change stuff is super noisy unfortunately, you can get notified many times for one actual file change, its due to using change tokens directly, we don't have any higher level concept that represents a 'file was updated' instead you get a bunch of file watcher notifications for changes that might not be the entire operation

@grahamehorner
Copy link
Contributor Author

@HaoK is there any way to dial down these events via configuration or are these fix in code, also I'm wondering if its possible to only receive the event if a given section changes as opposed to a file/set of files loaded by the configuration builder ?

@HaoK
Copy link
Member

HaoK commented Jun 3, 2019

Not really, the best thing to do to better control things might be to turn off the built in reload on change and manually watch for file changes and call reload on the config root yourself

@analogrelay
Copy link
Contributor

analogrelay commented Jun 3, 2019

is there any way to dial down these events via configuration

@HaoK can correct me if I'm wrong but I believe the chattiness here originates in the OS and how the app you use to edit the file writes to it. There really isn't much we can do. You could definitely add some logic to "debounce" the notifications though (i.e. wait a few seconds for the notifications to settle).

@HaoK
Copy link
Member

HaoK commented Jun 4, 2019

Correct, these notifications are coming from the file system abstractions directly, there's nothing we can really do at the config layer for this https://github.com/aspnet/Extensions/blob/master/src/FileProviders/Abstractions/src/IFileProvider.cs#L32

@HaoK
Copy link
Member

HaoK commented Jun 4, 2019

We already added some throttling in the reload logic to prevent spamming reloads, but it doesn't look that that helps a ton:

https://github.com/aspnet/Extensions/blob/master/src/Configuration/Config.FileExtensions/src/FileConfigurationProvider.cs#L37

@ghost
Copy link

ghost commented Nov 12, 2020

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

@ghost ghost closed this as completed Nov 12, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

No branches or pull requests

7 participants