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

Shutdown hook #25

Closed
borkdude opened this issue Oct 20, 2020 · 2 comments
Closed

Shutdown hook #25

borkdude opened this issue Oct 20, 2020 · 2 comments

Comments

@borkdude
Copy link
Contributor

borkdude commented Oct 20, 2020

Add option :shutdown-hook which defaults to (fn [proc] (.destroy (:proc proc))) and can be overridden with any arity-1 function.

@borkdude
Copy link
Contributor Author

Also see #19.

@borkdude
Copy link
Contributor Author

borkdude commented Oct 20, 2020

At work I have a script which didn't kill all subprocesses. I needed to do this:

(ns dev
  (:require [babashka.process :refer [process]]))

(defn child-process-handles [^java.lang.ProcessHandle x]
  (distinct
   (cons x
         (mapcat child-process-handles
                 (iterator-seq (.iterator (.descendants x)))))))

(defn kill [proc]
  (run! (fn [^java.lang.ProcessHandle handle]
          (prn (.pid handle))
          (.destroy handle))
        (child-process-handles (.toHandle (:proc proc)))))

(defn cljs []
  (let [p (process ["./clojure" "-A:frontend:cljs/dev"] {:out :inherit
                                                         :err :inherit})]
    (-> (Runtime/getRuntime)
        (.addShutdownHook (Thread. #(kill p))))))

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

1 participant