adding m/start-with
This commit is contained in:
parent
46d2f98c73
commit
09e7c78d22
4 changed files with 60 additions and 16 deletions
|
|
@ -14,11 +14,14 @@
|
||||||
;; [clojure.core.async :refer [>!! <!! >! <! go-loop alt! timeout]]
|
;; [clojure.core.async :refer [>!! <!! >! <! go-loop alt! timeout]]
|
||||||
[clojure.tools.namespace.repl :as tn]
|
[clojure.tools.namespace.repl :as tn]
|
||||||
[check.parts-test]
|
[check.parts-test]
|
||||||
|
[check.start-with-test]
|
||||||
[mount]
|
[mount]
|
||||||
[app :refer [create-nyse-schema find-orders add-order]])) ;; <<<< replace this your "app" namespace(s) you want to be available at REPL time
|
[app :refer [create-nyse-schema find-orders add-order]])) ;; <<<< replace this your "app" namespace(s) you want to be available at REPL time
|
||||||
|
|
||||||
(defn start []
|
(defn start []
|
||||||
(mount/start-without #'check.parts-test/should-not-start)) ;; example on how to start app without certain states
|
(mount/start-without #'check.start-with-test/test-conn
|
||||||
|
#'check.start-with-test/test-nrepl
|
||||||
|
#'check.parts-test/should-not-start)) ;; example on how to start app without certain states
|
||||||
|
|
||||||
(defn stop []
|
(defn stop []
|
||||||
(mount/stop))
|
(mount/stop))
|
||||||
|
|
|
||||||
|
|
@ -72,20 +72,33 @@
|
||||||
(map second)
|
(map second)
|
||||||
(filter mount-state?)))
|
(filter mount-state?)))
|
||||||
|
|
||||||
;; TODO: narrow down by {:mount {:include-ns
|
|
||||||
;; {:starts-with ["app.foo" "bar.baz"]
|
|
||||||
;; :nss ["app.nyse" "app.tools.datomic"] }
|
|
||||||
;; :exclude-ns
|
|
||||||
;; {:starts-with ["dont.want.this" "app.debug"]
|
|
||||||
;; :nss ["dev" "app.stage"]}}}
|
|
||||||
;;
|
|
||||||
;; would come from boot/lein dev profile
|
|
||||||
(defn- bring [states fun order]
|
(defn- bring [states fun order]
|
||||||
(->> states
|
(->> states
|
||||||
(sort-by (comp :order meta) order)
|
(sort-by (comp :order meta) order)
|
||||||
(map #(fun % (meta %)))
|
(map #(fun % (meta %)))
|
||||||
doall))
|
doall))
|
||||||
|
|
||||||
|
(defn- rollback! [state]
|
||||||
|
(let [{:keys [origin start stop sub?]} (meta state)]
|
||||||
|
(when origin
|
||||||
|
(alter-meta! state assoc :origin nil
|
||||||
|
:start (or (:start origin) start)
|
||||||
|
:stop (or (:stop origin) stop)))))
|
||||||
|
|
||||||
|
(defn- unsub [state]
|
||||||
|
(when (-> (meta state) :sub?)
|
||||||
|
(alter-meta! state assoc :sub? nil
|
||||||
|
:started false)))
|
||||||
|
|
||||||
|
(defn- substitute! [state with]
|
||||||
|
(let [{:keys [start stop] :as origin} (meta state)
|
||||||
|
m-with (meta with)]
|
||||||
|
(alter-meta! with assoc :sub? true :started? true) ;; pre: called by "start-with"
|
||||||
|
(alter-meta! state assoc :origin {:start start
|
||||||
|
:stop stop}
|
||||||
|
:start (:start m-with)
|
||||||
|
:stop (:stop m-with))))
|
||||||
|
|
||||||
(defn start [& states]
|
(defn start [& states]
|
||||||
(let [states (or (seq states) (find-all-states))]
|
(let [states (or (seq states) (find-all-states))]
|
||||||
(bring states up <)
|
(bring states up <)
|
||||||
|
|
@ -93,7 +106,9 @@
|
||||||
|
|
||||||
(defn stop [& states]
|
(defn stop [& states]
|
||||||
(let [states (or states (find-all-states))]
|
(let [states (or states (find-all-states))]
|
||||||
|
(doall (map unsub states)) ;; unmark substitutions marked by "start-with"
|
||||||
(bring states down >)
|
(bring states down >)
|
||||||
|
(doall (map rollback! states)) ;; restore to origin from "start-with"
|
||||||
:stopped))
|
:stopped))
|
||||||
|
|
||||||
(defn start-with-args [xs & states]
|
(defn start-with-args [xs & states]
|
||||||
|
|
@ -103,12 +118,10 @@
|
||||||
(start)))
|
(start)))
|
||||||
|
|
||||||
(defn start-with [with]
|
(defn start-with [with]
|
||||||
(if (seq with)
|
|
||||||
(let [app (find-all-states)]
|
(let [app (find-all-states)]
|
||||||
;; needs more thinking on merging, since the ns should not change
|
(doall
|
||||||
;; because it could be used in other states, so only start/stop need to be merged
|
(for [[from to] with]
|
||||||
(warn "substituting states is not _yet_ implemented")
|
(substitute! from to)))
|
||||||
(start))
|
|
||||||
(start)))
|
(start)))
|
||||||
|
|
||||||
(defn start-without [& states]
|
(defn start-without [& states]
|
||||||
|
|
|
||||||
28
test/check/start_with_test.clj
Normal file
28
test/check/start_with_test.clj
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
(ns check.start-with-test
|
||||||
|
(:require [mount :refer [defstate] :as m]
|
||||||
|
[app.config :refer [app-config]]
|
||||||
|
[app.nyse :refer [conn]]
|
||||||
|
[app :refer [nrepl]]
|
||||||
|
[clojure.test :refer :all]))
|
||||||
|
|
||||||
|
(defstate test-conn :start (long 42)
|
||||||
|
:stop (constantly 0))
|
||||||
|
|
||||||
|
(defstate test-nrepl :start (vector))
|
||||||
|
|
||||||
|
(deftest start-with
|
||||||
|
|
||||||
|
(testing "should start with substitutes"
|
||||||
|
(let [_ (m/start-with {#'app.nyse/conn #'check.start-with-test/test-conn
|
||||||
|
#'app/nrepl #'check.start-with-test/test-nrepl})]
|
||||||
|
(is (map? app-config))
|
||||||
|
(is (vector? nrepl))
|
||||||
|
(is (= conn 42))
|
||||||
|
(mount/stop)))
|
||||||
|
|
||||||
|
(testing "should start normally after start-with"
|
||||||
|
(let [_ (m/start)]
|
||||||
|
(is (map? app-config))
|
||||||
|
(is (instance? clojure.tools.nrepl.server.Server nrepl))
|
||||||
|
(is (instance? datomic.peer.LocalConnection conn))
|
||||||
|
(mount/stop))))
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
(ns check.without-test
|
(ns check.start-without-test
|
||||||
(:require [mount :as m]
|
(:require [mount :as m]
|
||||||
[app.config :refer [app-config]]
|
[app.config :refer [app-config]]
|
||||||
[app.nyse :refer [conn]]
|
[app.nyse :refer [conn]]
|
||||||
Loading…
Reference in a new issue