Add :shutdown op
This commit is contained in:
parent
f25476e146
commit
4e70b92584
3 changed files with 19 additions and 4 deletions
|
|
@ -178,6 +178,12 @@ message related to the request with `id` `1d17f8fe-4f70-48bf-b6a9-dc004e52d056`.
|
||||||
|
|
||||||
Now you know most there is to know about the pod protocol!
|
Now you know most there is to know about the pod protocol!
|
||||||
|
|
||||||
|
#### shutdown
|
||||||
|
|
||||||
|
When babashka is about to exit, it sends a `{"op" "shutdown"}` message and waits
|
||||||
|
for the pod process to end. This gives the pod a chance to clean up resources
|
||||||
|
before it exits.
|
||||||
|
|
||||||
#### out and err
|
#### out and err
|
||||||
|
|
||||||
Pods may send messages with an `out` and `err` string value. Babashka prints
|
Pods may send messages with an `out` and `err` string value. Babashka prints
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,9 @@
|
||||||
(binding [*out* @sci/err]
|
(binding [*out* @sci/err]
|
||||||
(prn e))))))
|
(prn e))))))
|
||||||
|
|
||||||
|
(defn next-id []
|
||||||
|
(str (java.util.UUID/randomUUID)))
|
||||||
|
|
||||||
(defn invoke [pod pod-var args async?]
|
(defn invoke [pod pod-var args async?]
|
||||||
(let [stream (:stdin pod)
|
(let [stream (:stdin pod)
|
||||||
format (:format pod)
|
format (:format pod)
|
||||||
|
|
@ -82,7 +85,7 @@
|
||||||
write-fn (case format
|
write-fn (case format
|
||||||
:edn pr-str
|
:edn pr-str
|
||||||
:json cheshire/generate-string)
|
:json cheshire/generate-string)
|
||||||
id (str (java.util.UUID/randomUUID))
|
id (next-id)
|
||||||
chan (async/chan)
|
chan (async/chan)
|
||||||
_ (swap! chans assoc id chan)
|
_ (swap! chans assoc id chan)
|
||||||
_ (write stream {"id" id
|
_ (write stream {"id" id
|
||||||
|
|
@ -105,8 +108,13 @@
|
||||||
stdin (.getOutputStream p)
|
stdin (.getOutputStream p)
|
||||||
stdout (.getInputStream p)
|
stdout (.getInputStream p)
|
||||||
stdout (java.io.PushbackInputStream. stdout)
|
stdout (java.io.PushbackInputStream. stdout)
|
||||||
_ (add-shutdown-hook! #(.destroy p))
|
_ (add-shutdown-hook!
|
||||||
_ (write stdin {"op" "describe"})
|
(fn []
|
||||||
|
(write stdin {"op" "shutdown"
|
||||||
|
"id" (next-id)})
|
||||||
|
(.waitFor p)))
|
||||||
|
_ (write stdin {"op" "describe"
|
||||||
|
"id" (next-id)})
|
||||||
reply (read stdout)
|
reply (read stdout)
|
||||||
format (-> (get reply "format") bytes->string keyword)
|
format (-> (get reply "format") bytes->string keyword)
|
||||||
pod {:process p
|
pod {:process p
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,8 @@
|
||||||
(write
|
(write
|
||||||
{"status" ["done"]
|
{"status" ["done"]
|
||||||
"id" id})))
|
"id" id})))
|
||||||
(recur)))))))))
|
(recur))
|
||||||
|
:shutdown (System/exit 0))))))))
|
||||||
|
|
||||||
(let [cli-args (set *command-line-args*)]
|
(let [cli-args (set *command-line-args*)]
|
||||||
(if (contains? cli-args "--run-as-pod")
|
(if (contains? cli-args "--run-as-pod")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue