GeeWee / BetterHostedServices

Fixed a wide variety of issues that have to do with Hosted Services and BackgroundServices, such as error handling and the abillity to access them via the DI.
MIT License
27 stars 4 forks source link

End process *less* gracefully #19

Open lwestfall opened 3 years ago

lwestfall commented 3 years ago

Describe the feature

It'd be nice if there was an option to end the process less gracefully as opposed to IHostApplicationLifetime.StopApplication()

But.... why?

In my scenario, I'm trying to apply this to a Windows service, which has recovery options in the event of a service failure. In my case specifically I want Windows to restart the service (after a short delay) when it encounters an exception. When implementing this packages as-is, it fails successfully in a way that the recovery options don't trigger.

To be honest I'm not sure exactly the best way to proceed. I'm not sure if using a non-zero exit code would help. I can run some tests tomorrow to see what options are available.

This site seems to suggest that a non-zero exit code won't trigger this, but it was written in 2008 so maybe things have changed. https://mdenomy.wordpress.com/2008/02/28/using-the-automatic-recovery-features-of-windows-services/

PS nice work! I really like the simplicity and I'm glad someone put a workaround in place until 6.0 (hopefully) adds the stop-on-exception feature that this addresses.

GeeWee commented 3 years ago

Thanks for the kind words. And yes, here's hoping we can deprecate this for 6.0!

If you want to try to disable to application in another way there's actually a reasonably simple hook for that. Instead of calling serviceCollection.AddBetterHostedServices(), you can simply call serviceCollection.AddTransient<IApplicationEnder, YourCoolStrategyForEndingApplications>(); - and then experiment with whatever strategy you think is a good fit.

There's a bit of wonkyness with how .NET awaits the IHostedService (it doesn't) so I actually haven't been able to make it exit less gracefully than with StopApplication - at least not in test scenarios, but that could be the test server.

If you figure out a good solution, I would love to know it so we can share it among others as well!

lwestfall commented 3 years ago

Alright - I have an update!

It turns out that using Environment.Exit() rather than IHostApplicationLifetime.StopApplication() does exactly what I was looking for, with the added bonus that a Windows error event is included in the event viewer (not a requirement in my case, but definitely nice!). My only issue is that it exits so quickly that the log buffer isn't getting flushed by the time the process ends, so whatever exception causes the shutdown doesn't get logged (but that's another issue).

using System;
using BetterHostedServices;

public class UncleanApplicationEnder : IApplicationEnder
{
    public void ShutDownApplication()
    {
        // exit uncleanly with code 1
        // this should trigger Windows service recovery (if configured in SCM)
        Environment.Exit(1);
    }
}

Out of curiosity, are you at all interested in having this alternative ApplicationEnder merged into your repo? If so I can add some docs and submit a PR, otherwise I'm fine with just keeping this custom implementation local and I think you can close the issue.

GeeWee commented 3 years ago

I would absolutely love it if you'd submit a PR to merge it in and document it (particularly the log-buffer issue)

lwestfall commented 3 years ago

Cool! I might have to get to it this weekend, had a lot of other issues crop up today.

One quick question though: in order to deal with my logging issues, maybe we should include an optional "BeforeShutdown" hook in IApplicationEnder so a function can be fed to it at runtime, which will be called by ShutdownApplication() before exiting. This will allow people like me to have a custom function for dealing with their log buffer (and do any other tidying up as necessary) before forcing the shutdown.

I can include this hook in the PR along with adding it to your sample program, does that sound good to you or do you have any other thoughts?

GeeWee commented 3 years ago

No rush.

I'm not sure whether it's a cleaner API to have a "shutdown" hook or just tell people to subclass the ApplicationEnder, do their own cleanup and then call base.ShutdownApplication() - I'm okay with whatever API you come up with I think as both are good suggestions.