Last active
January 7, 2016 01:41
-
-
Save moea/6871166bb1fdb462efe4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn exec-chan | |
"spawns a child process for cmd with args. routes stdout, stderr, and | |
the exit code to a channel. returns the channel immediately." | |
[cmd args & [{:keys [chan]}]] | |
(let [c (or chan (a/chan)), p (spawn cmd args)] | |
(.on (.-stdout p) "data" #(put! c [:out (str %)])) | |
(.on (.-stderr p) "data" #(put! c [:err (str %)])) | |
(.on p "close" #(a/close! c)) | |
c)) | |
(defn exec [[cmd & args] & [{:keys [timeout chan] :or {timeout 2000}}]] | |
(let [c (exec-chan cmd (clj->js args)) | |
out-chan (or chan (a/chan))] | |
(go | |
(loop [] | |
(let [v (alt! | |
(a/timeout timeout) ::timeout | |
c ([v] v))] | |
(when (not= v ::timeout) | |
(>! out-chan v) | |
(recur)))) | |
(a/close! out-chan))) | |
out-chan) | |
(defn test-fn [] | |
(go | |
(let [result (<! (a/into [] (exec ["./test.sh" "-d" "3"])))] | |
(println result)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment