diff --git a/doc/pods.md b/doc/pods.md index 4f4ab051..05d7a2c1 100644 --- a/doc/pods.md +++ b/doc/pods.md @@ -121,7 +121,8 @@ The pod should reply to this request with a message in the vein of: {"format" "json" "namespaces" [{"name" "pod.lispyclouds.sqlite" - "vars" [{"name" "execute!"}]}]} + "vars" [{"name" "execute!"}]}] + "ops" {"shutdown" {}}} ``` In this reply, the pod declares that payloads will be encoded and decoded using @@ -133,6 +134,12 @@ this message from the pod's stdout. Upon receiving this message, babashka creates these namespaces and vars. +The optional `ops` value communicates which ops the pod supports, beyond +`describe` and `invoke`. It is a map of op names and option maps. In the above +example the pod declares that it supports the `shutdown` op. Since the +`shutdown` op does not need any additional options right now, the value is an +empty map. + As a babashka user, you can load the pod with: ``` clojure @@ -180,7 +187,8 @@ 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 +When babashka is about to exit, it sends an `{"op" "shutdown"}` message, if the +pod has declared that it supports it in the `describe` response. Then it waits for the pod process to end. This gives the pod a chance to clean up resources before it exits. diff --git a/examples/pods/pod-babashka-filewatcher/src/main.rs b/examples/pods/pod-babashka-filewatcher/src/main.rs index 958c34ee..74b0bd70 100644 --- a/examples/pods/pod-babashka-filewatcher/src/main.rs +++ b/examples/pods/pod-babashka-filewatcher/src/main.rs @@ -105,10 +105,6 @@ fn handle_incoming(val: bc::Value) { _ => panic!(var) }; }, - "shutdown" => { - // TODO: clean up stuff? - std::process::exit(0); - } _ => panic!(op) } } diff --git a/examples/pods/pod-babashka-hsqldb/src/pod/babashka/hsqldb.clj b/examples/pods/pod-babashka-hsqldb/src/pod/babashka/hsqldb.clj index 5c914171..6345ce31 100644 --- a/examples/pods/pod-babashka-hsqldb/src/pod/babashka/hsqldb.clj +++ b/examples/pods/pod-babashka-hsqldb/src/pod/babashka/hsqldb.clj @@ -33,7 +33,8 @@ (case op :describe (do (write {"format" "edn" "namespaces" [{"name" "pod.babashka.hsqldb" - "vars" [{"name" "execute!"}]}]}) + "vars" [{"name" "execute!"}]}] + "ops" {"shutdown" {}}}) (recur)) :invoke (let [var (-> (get message "var") read-string diff --git a/examples/pods/pod-lispyclouds-sqlite/pod-lispyclouds-sqlite.py b/examples/pods/pod-lispyclouds-sqlite/pod-lispyclouds-sqlite.py index 747c288f..0b0c65cc 100755 --- a/examples/pods/pod-lispyclouds-sqlite/pod-lispyclouds-sqlite.py +++ b/examples/pods/pod-lispyclouds-sqlite/pod-lispyclouds-sqlite.py @@ -56,8 +56,6 @@ def main(): conn.commit() conn.close() - elif op == "shutdown": - sys.exit(0) if __name__ == "__main__": main() diff --git a/sci b/sci index 806cf61e..c55a1ef6 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 806cf61eeff31869727e205a8f1a7021a0ea5b49 +Subproject commit c55a1ef672c82a06f540e91c138d5689b679e02d diff --git a/src/babashka/impl/pods.clj b/src/babashka/impl/pods.clj index 902bf0b0..b22ef750 100644 --- a/src/babashka/impl/pods.clj +++ b/src/babashka/impl/pods.clj @@ -108,21 +108,25 @@ stdin (.getOutputStream p) stdout (.getInputStream p) stdout (java.io.PushbackInputStream. stdout) - _ (add-shutdown-hook! - (fn [] - (write stdin {"op" "shutdown" - "id" (next-id)}) - (.waitFor p))) _ (write stdin {"op" "describe" "id" (next-id)}) reply (read stdout) format (-> (get reply "format") bytes->string keyword) + ops (some->> (get reply "ops") keys (map keyword) set) pod {:process p :pod-spec pod-spec :stdin stdin :stdout stdout :chans (atom {}) - :format format} + :format format + :ops ops} + _ (add-shutdown-hook! + (fn [] + (if (contains? ops :shutdown) + (do (write stdin {"op" "shutdown" + "id" (next-id)}) + (.waitFor p)) + (.destroy p)))) pod-namespaces (get reply "namespaces") vars-fn (fn [ns-name-str vars] (reduce diff --git a/test-resources/pod.clj b/test-resources/pod.clj index 48824283..b07ef29f 100644 --- a/test-resources/pod.clj +++ b/test-resources/pod.clj @@ -60,7 +60,8 @@ {"name" "assoc"} {"name" "error"} {"name" "print"} - {"name" "print-err"}]}]}) + {"name" "print-err"}]}] + "ops" {"shutdown" {}}}) (recur)) :invoke (let [var (-> (get message "var") read-string