pods: ops value in describe

This commit is contained in:
Michiel Borkent 2020-05-07 23:56:03 +02:00
parent 859529b44b
commit 67639183eb
7 changed files with 25 additions and 17 deletions

View file

@ -121,7 +121,8 @@ The pod should reply to this request with a message in the vein of:
{"format" "json" {"format" "json"
"namespaces" "namespaces"
[{"name" "pod.lispyclouds.sqlite" [{"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 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. 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: As a babashka user, you can load the pod with:
``` clojure ``` clojure
@ -180,7 +187,8 @@ Now you know most there is to know about the pod protocol!
#### shutdown #### 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 for the pod process to end. This gives the pod a chance to clean up resources
before it exits. before it exits.

View file

@ -105,10 +105,6 @@ fn handle_incoming(val: bc::Value) {
_ => panic!(var) _ => panic!(var)
}; };
}, },
"shutdown" => {
// TODO: clean up stuff?
std::process::exit(0);
}
_ => panic!(op) _ => panic!(op)
} }
} }

View file

@ -33,7 +33,8 @@
(case op (case op
:describe (do (write {"format" "edn" :describe (do (write {"format" "edn"
"namespaces" [{"name" "pod.babashka.hsqldb" "namespaces" [{"name" "pod.babashka.hsqldb"
"vars" [{"name" "execute!"}]}]}) "vars" [{"name" "execute!"}]}]
"ops" {"shutdown" {}}})
(recur)) (recur))
:invoke (let [var (-> (get message "var") :invoke (let [var (-> (get message "var")
read-string read-string

View file

@ -56,8 +56,6 @@ def main():
conn.commit() conn.commit()
conn.close() conn.close()
elif op == "shutdown":
sys.exit(0)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

2
sci

@ -1 +1 @@
Subproject commit 806cf61eeff31869727e205a8f1a7021a0ea5b49 Subproject commit c55a1ef672c82a06f540e91c138d5689b679e02d

View file

@ -108,21 +108,25 @@
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!
(fn []
(write stdin {"op" "shutdown"
"id" (next-id)})
(.waitFor p)))
_ (write stdin {"op" "describe" _ (write stdin {"op" "describe"
"id" (next-id)}) "id" (next-id)})
reply (read stdout) reply (read stdout)
format (-> (get reply "format") bytes->string keyword) format (-> (get reply "format") bytes->string keyword)
ops (some->> (get reply "ops") keys (map keyword) set)
pod {:process p pod {:process p
:pod-spec pod-spec :pod-spec pod-spec
:stdin stdin :stdin stdin
:stdout stdout :stdout stdout
:chans (atom {}) :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") pod-namespaces (get reply "namespaces")
vars-fn (fn [ns-name-str vars] vars-fn (fn [ns-name-str vars]
(reduce (reduce

View file

@ -60,7 +60,8 @@
{"name" "assoc"} {"name" "assoc"}
{"name" "error"} {"name" "error"}
{"name" "print"} {"name" "print"}
{"name" "print-err"}]}]}) {"name" "print-err"}]}]
"ops" {"shutdown" {}}})
(recur)) (recur))
:invoke (let [var (-> (get message "var") :invoke (let [var (-> (get message "var")
read-string read-string