Fork me on GitHub
#mount
<
2016-12-02
>
mike_ananev09:12:15

hi there! how to start all states in current namespace only?

mike_ananev09:12:32

i have 10 or more states and want to avoid enumerate them all

mike_ananev09:12:48

(mount/start #'foo #'bar ....)

tolitius19:12:29

@mike1452: hey Mike, mount does not have API to scope the states by a namespace, but you can easily do it by:

dev=> (require '[clojure.string :as s])

dev=> (defn start-ns [ns]
       (->> (mount/find-all-states)
            (filter #(s/starts-with? % ns))
            mount/start))

dev=> (start-ns "#'neo.conf")
INFO  neo.conf - loading config from dev/resources/config.edn
{:started ["#'neo.conf/config"]}

tolitius20:12:14

I just opened a mount/find-all-states function in 0.1.11-SNAPSHOT. (it used to be private)

tolitius21:12:16

sure, let me know if it works for you

mike_ananev21:12:05

@tolitius is it correct way to check?

tolitius21:12:58

dev=> ((mount/running-states) "#'app.db/conn")
nil

dev=> (mount/start)
{:started ["#'app.conf/config" "#'app.db/conn" "#'app.www/nyse-app" "#'app.example/nrepl"]}

dev=> ((mount/running-states) "#'app.db/conn")
"#'app.db/conn"
this is a bit cleaner

mike_ananev21:12:41

@tolitius If i have states: config, conn and obj - all of them in current namespace. And I want to not start conn if config not started. And not start obj if conn is not started. Under "not started" i mean any case that is not success in terms of state. For example, if I've got Exception during conn - it is "not success" cause resource is unavailable. What value should I put to conn state if "not success"? (mount.core.NotStartedState. "conn") ? Have you some semantics for values? So and I want to check in depended states if state is not started then I log error. "bit cleaner" variant is not handy for such case.

mike_ananev21:12:17

or it is error state and I should introduce some values for "error state" and check for "not started" and "error state"?

tolitius21:12:58

I am still digesting your use case.. give me a couple of seconds

tolitius21:12:13

let's say you have 3 states: config, conn and obj the problem you're trying to solve is: * not to start conn on case you could not start conf * not to start obj on case you could not start conn ?

mike_ananev21:12:28

not start conn if conf not-started or in error state

tolitius21:12:07

in case we are talking about just (mount/start ...)

not start conn if conf in error state
is taken care of, since mount will stop in case there is an error starting conf
not start conn if conf not-started state
should also be handled in case conf is injected in conn

tolitius21:12:30

so, are you talking about starting parts of your app in steps?

mike_ananev21:12:53

"since mount will stop in case there is an error starting" - that is what I want, but didn't catch.

mike_ananev21:12:34

I put intentionally 1/0

mike_ananev21:12:20

and config and all depended states will be in started state but iti is not true

mike_ananev22:12:02

mount didn't catch that i've got and exeception

tolitius22:12:22

boot.user=> (defstate a :start (/ 1 0))
#'boot.user/a
boot.user=> (defstate b :start (println "starting a with b:" b))
#'boot.user/b
boot.user=> (mount/start)

   java.lang.RuntimeException: could not start [#'boot.user/a] due to
java.lang.ArithmeticException: Divide by zero
boot.user=>

boot.user=> (mount/running-states)
#{}

mike_ananev22:12:55

hmmm, I will check right now ones more

tolitius22:12:04

boot.user=> (defstate ^:dynamic config :start (do (/ 1 0) (println "config is started")))
#'boot.user/config
boot.user=> (defstate db :start (println "starting db with config" config))
#'boot.user/db
boot.user=> (mount/start)

   java.lang.RuntimeException: could not start [#'boot.user/config] due to
java.lang.ArithmeticException: Divide by zero
boot.user=> (mount/running-states)
#{}

mike_ananev22:12:17

no such var mount/running-states

mike_ananev22:12:42

is it in 0.1.11 ?

tolitius22:12:16

ah.. it could be yes

tolitius22:12:37

might need to release it soon, since there are several small fixes there as well

tolitius22:12:00

0.1.11-SNAPSHOT

mike_ananev22:12:28

ok, cool. mount will not start if got an unhandled Exception

mike_ananev22:12:44

but, what if I have try-catch block inside and process Exception my self. What value should i return in order to tell mount this is not correct state?

tolitius22:12:57

in case you want to signal a problem don't do try/catch, or if you need to do it re throw the exception

tolitius22:12:00

sure, good luck 🙂