Code Monkey home page Code Monkey logo

Comments (5)

GeeWee avatar GeeWee commented on July 20, 2024

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!

from betterhostedservices.

lwestfall avatar lwestfall commented on July 20, 2024

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.

from betterhostedservices.

GeeWee avatar GeeWee commented on July 20, 2024

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

from betterhostedservices.

lwestfall avatar lwestfall commented on July 20, 2024

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?

from betterhostedservices.

GeeWee avatar GeeWee commented on July 20, 2024

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.

from betterhostedservices.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.