Branched off from #51
Intended for NServiceBus 6.x and NServiceBus.Host 7.x, not for milestone NSBv6
The purpose of this feature is not to influence the actual termination of the process, but more to educate developers what is happening and how to properly take actions.
On shutdown/reboot of the system there's a set amount of time a Windows Service has to gracefully shut down. Not much to do about it.
When shutting down manually with the MMC services snapin however, we have 125 seconds to shut down. After that, the service is marked is hanging and will likely never stop. We can influence this by requesting more time. More documentation can be found on MSDN.
Logging
We should provide better logging on what is happening. We need to log that we are trying to properly shut down and when we need more time of Windows. Perhaps x-amount of times before (we think) the service will actually be terminated by Windows.
When host is killed by Windows, non-transactional transports & persistence can loose messages and we should notify users that this might have happened.
When operations needs to kill the process, it's convenient when the process-id is in the log messages as well. We can inform inside the log this is the process to kill, if needed.
Requesting more time
We should decide if requesting more time is necessary or if it is better that the system notifies operations of a 'hanging' Windows Service. That way operations can be notified by monitoring systems and look at the logfiles what is actually happening.
The idea is that users are able to dump the process to disk and send it to us for debugging purposes. If a service is actually never gracefully shutting down, we might be on the a bug somewhere else and would very much like the information. It's also possible to include additional packages to log to Raygun, for example.
Registry access
We can read the amount of time we have from the registry in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
and the WaitToKillServiceTimeout
value. We can set the GenericHost to a default amount of time and the WindowsHost is be able to modify this, according to registry setting. The available amount of time on system shutdown differs in different Windows versions. The reason for reading registry in WindowsHost is because we want to be able to support .NET Core in the future.