I heard someone mention the use of ShutdownGuard for long running precesses.
How does one properly make use of this and what are some cautions around its usage?
The ShutdownGuard
prevents the application from shutting down, primarily this was meant to be used by the package installer to prevent the application from shutting down due to config files and dll's being deployed. Without this in place, the application would potentially restart several times during the package installation process, once each time a config file or dll was deployed.
The ShutdownGuard
disposable class can be found in the Sitecore.Update.Installer.Installer.Utils
namespace, within the Sitecore.Update.dll
.
You could use this in your own code by wrapping it within a using
block:
//using Sitecore.Update.Installer.Utils;
using (new ShutdownGuard())
{
// your custom code for long running process
}
BUT, given the namespace and library this class is implemented within, it was clearly not intended for this code to be used for preventing the application shutting down during long running processes and tasks. This feels like a code smell and a misuse.
You should use Sitecore jobs to invoke long-running process in background threads (read more here and here). If you need to run tasks periodically then you should used Scheduled Tasks.
Any application restarts whilst the Job/Task is running will cause it to not complete, but your code should take this into consideration and be defensively coded to allow the task to gracefully restart at a later stage and pick up where it left off. Relying on a code-only solution to prevent application restarts would lead to issues if the application stopped for other reasons, such as the entire server itself shutting down or restarting (e.g. planned maintenance).