Fork me on GitHub
#babashka
<
2023-09-05
>
danm13:09:09

I've found some interesting backlog chat from this channel online about handling signals in babashka. At the time sun.misc.Signal was referenced, back in 2022. But I've found other stuff online that talks about this being "the old way to handle signals, pre Java-9". Is there a better way to do it now, or is that still the best idea?

borkdude13:09:45

This should now work, not sure what the new Java9+ way should be

danm13:09:48

Yeah, I was also having trouble working out what (if anything) a new version should use...

danm13:09:54

Hmm. Looks like for my use case (cleanup on SIGTERM before SIGKILL) I can just do

(-> (Runtime/getRuntime)
    (.addShutdownHook (Thread. ^Runnable (fn [] (println "Weeeee")))))

danm13:09:13

(Replace println with actual shutdown code ofc)

danm13:09:39

I knew that was the java way, I wasn't 100% it would work in bb, but it does 🙂

danm13:09:13

And I think the conversation about sun.misc.Signal being the old way was specifically for handling shutdown signals, rather than anything else

danm14:09:51

Hmm. How will that play with BABASHKA_DISABLE_SIGNAL_HANDLERS?

borkdude14:09:48

That env var will just not register babashka's own signal handlers

borkdude14:09:55

but you can still do your own thing

danm14:09:08

Aah cool, ta 🙂

Jacob O'Bryant17:09:48

This isn't an important suggestion at all, but borkdude I'm curious if you've ever thought of adding a flag for running a script stored at a url? like bb --url . It would be equivalent to bb -e '(load-string (slurp "https:/example.com/some-script.bb"))', just a bit prettier. Of course there's the question of whether it's actually a good idea to make running scripts off the internet more convenient--maybe a little more verbosity in that case is better anyway 😉

borkdude17:09:23

You can do this using -Sdeps + -m or -x as well

borkdude17:09:52

instead of curl you could use babashka.http-client (curl is fine as well though)

borkdude17:09:29

gotta run now, ttyl!

borkdude17:09:57

you can also let users install your tool via brew/scoop (see neil for an example of this) or use #C0400TZENE7 (easier for existing bb users)

💡 2
Jacob O'Bryant17:09:09

👍 I think -e '(load-string (slurp ...))' is probably still the least verbose of these options; I'll likely just go with that. Thanks!

🎉 2