Skip to content
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

Use Ctrl+C instead of allowing any key press to kill the process #931

Open
MangelMaxime opened this issue Jul 13, 2024 · 6 comments
Open

Comments

@MangelMaxime
Copy link

Using Ctrl+C is the standard to kill a CLI application.

Would you be ok with using that instead of allowing any key press to kill it?

My reason is that it is easy to press a key while the terminal is in focus which kill the process and then user needs to restart the server and wait for it to be available.

@nojaf
Copy link
Collaborator

nojaf commented Aug 5, 2024

Sounds reasonable. @baronfel, what is the recommended way in dotnet to handle this nowadays? I have a feeling we could use some sort of application host for this. Does that sound familiar?

@baronfel
Copy link
Collaborator

baronfel commented Aug 5, 2024

The way of least resistance today is the Generic Host, specifically Host Shutdown.

But that requires you buying in to the hostbuilder model. If you don't want all that, you can register to the shutdown/term signals yourself.

@nojaf
Copy link
Collaborator

nojaf commented Aug 6, 2024

Generic host seems reasonable, it could improve the logging story as well.

@MangelMaxime
Copy link
Author

Another solution is to do:

let private keepAlive () =

    // Keep the program alive until the user presses Ctrl+C
    Console.CancelKeyPress.AddHandler(fun _ ea ->
        ea.Cancel <- true
        Log.info "Received Ctrl+C, shutting down..."
        exit 0
    )

    while true do
        Console.ReadKey(true) |> ignore

Regarding the logging story, we could do something similar to what was done in Fable. Just create an instance of ILogger and use that instead of printfn ?

If going with Generic host, I am not sure where it should take place.

Should it replace the CoreBuildOptions.Execute method?

member this.Execute() =

And we map the start / stop / clean up, to the IHostedService methods?

@nojaf
Copy link
Collaborator

nojaf commented Aug 14, 2024

while true do there must be a better way right 🙈.

Regarding the logging story, we could do something similar to what was done in Fable.

Yes, that be great.

If going with Generic host, I am not sure where it should take place.

Yeah, I'm a little fuzzy on that too. I would suspect creating a new IHostedService and parse the arguments inside the StartAsync to then call BuildCommand or WatchCommand.

@MangelMaxime
Copy link
Author

while true do there must be a better way right 🙈.

while (not false) do 😀

More seriously, in general this what you do to keep a program alive and I suspect this is how the IHostedService does it under the hood.

If going with Generic host, I am not sure where it should take place.

Yeah, I'm a little fuzzy on that too. I would suspect creating a new IHostedService and parse the arguments inside the StartAsync to then call BuildCommand or WatchCommand.

Seems close to what I suppose is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants