l3nz / cli-matic

Compact, hands-free [sub]command line parsing library for Clojure.
Eclipse Public License 2.0
361 stars 29 forks source link

CLI-matic with http-kit server #66

Closed MafcoCinco closed 5 years ago

MafcoCinco commented 5 years ago

I've run into an issue with http-kit server and CLI-matic. My project uses CLI-matic to implement multiple entry points via sub-commands for use in a Kubernetes/Docker environment. Each sub-command represents a different service and is used as a different entry-point for a Docker container.

The problem I encountered was that CLI-matic seems to run exit handling as soon as run-cmd* completes. This causes my http-kit service to immediately exit after run-server is called as the server is launched asynchronously, returning a function that can be used in the main thread to shut down the service, which then promptly happens.

I have worked around this by adding (while true) after my call to run-server but this is less than ideal. It seems to me to be a code smell to need to include this in every service sub-command and additionally, my on-shutdown handler does not seem to be called correctly with this work around. That was surprising to me and perhaps is related to something else that I'm not thinking of.

Either way, any thoughts on how best to handle this use case would be greatly appreciated. Thanks for your time.

l3nz commented 5 years ago

I think that this is the intended behavior; when "run-cmd" terminates, everything goes down. So I would wait until you are ready to close to return from run-cmd.

MafcoCinco commented 5 years ago

Fair enough. I have a PR ready to add this by adding a tag to the sub-command map called keep-alive which will not call the termination. Sounds like a I should not bother, correct?

l3nz commented 5 years ago

I think so 😄 - but I thank you for taking the time to submit an idea and hacking on it!

MafcoCinco commented 5 years ago

Sure thing. Thanks for your response. Much appreciated!

rgm commented 4 years ago

A slightly less hard-on-the-cpu action that seems to work for me is to use core.async and stick (let [c (a/chan)] (a/<!! c)) at the bottom of your :runs fn.

I agree though: this and (while true) seem a bit hacky. I'd 👍 either a way to declare that the JVM should wait around in the command map, or get this into the docs as a gotcha.

l3nz commented 4 years ago

@rgm @MafcoCinco see #84