Skip to content

Instantly share code, notes, and snippets.

@wactbprot
Created May 30, 2020 15:15
Show Gist options
  • Save wactbprot/0303a6325c6d22a03d0c04ed0dadc88c to your computer and use it in GitHub Desktop.
Save wactbprot/0303a6325c6d22a03d0c04ed0dadc88c to your computer and use it in GitHub Desktop.
start, stop, error, clojure, core.async
(ns cmp.system
^{:author "wactbprot"
:doc "study."}
(:require [taoensso.timbre :as timbre]
[clojure.core.async :as a]))
;;------------------------------
;; kill channel
;;------------------------------
(def kill> (a/chan))
;;------------------------------
;; error channel
;;------------------------------
(def err< (a/chan))
(defn error-handler
[err<]
(a/go-loop []
(let [e (a/<! err<)]
(if (string? e)
(timbre/error e)
(timbre/error (.getMessage e))))
(recur)))
(defn launch
[in> dispatch-fn kill> err<]
(a/go-loop [coll (a/alts! [kill> in>] :priority true)]
(let [[x ch] coll]
(if (= ch kill>)
(timbre/warn "receive msg in kill> channel; shutdown")
(do
(try
(dispatch-fn x)
(catch Exception e
(a/>! err< e)))
(recur (a/alts! [kill> in>] :priority true)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment