How to best shut down a clojure core.async pipeline of processes -
i have clojure processing app pipeline of channels. each processing step computations asynchronously (ie. makes http request using http-kit or something), , puts result on output channel. way next step can read channel , computation.
my main function looks this
(defn -main [args] (-> file/tmp-dir (schedule/scheduler) (search/searcher) (process/resultprocessor) (buy/buyer) (report/reporter)))
currently, scheduler step drives pipeline (it hasn't got input channel), , provides chain workload.
when run in repl:
(-main "some args")
it runs forever due infinity of scheduler. best way change architecture such can shut down whole system repl? closing each channel means system terminates?
would broadcast channel help?
you have scheduler alts!
/ alts!!
on kill channel , input channel of pipeline:
(def kill-channel (async/chan)) (defn scheduler [input output-ch kill-ch] (loop [] (let [[v p] (async/alts!! [kill-ch [out-ch (preprocess input)]] :priority true)] (if-not (= p kill-ch) (recur))))
putting value on kill-channel
terminate loop.
technically use output-ch
control process (puts closed channels return false
), find explicit kill channels cleaner, @ least top-level pipelines.
to make things simultaneously more elegant , more convenient use (both @ repl , in production), use stuart sierra's component, start scheduler loop (on separate thread) , assoc
kill channel on component in component's start
method , close!
kill channel (and thereby terminate loop) in component's stop
method.
Comments
Post a Comment