mount

mike_ananev 2016-12-02T09:33:15.001355Z

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

mike_ananev 2016-12-02T09:34:32.001356Z

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

mike_ananev 2016-12-02T09:34:48.001357Z

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

tolitius 2016-12-02T19:59:29.001367Z

@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"]}

tolitius 2016-12-02T20:00:14.001368Z

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

mike_ananev 2016-12-02T21:04:27.001370Z

@tolitius Thanx! 🙂

tolitius 2016-12-02T21:05:16.001371Z

sure, let me know if it works for you

mike_ananev 2016-12-02T21:20:05.001373Z

@tolitius is it correct way to check?

tolitius 2016-12-02T21:23:58.001374Z

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_ananev 2016-12-02T21:33:41.001376Z

@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_ananev 2016-12-02T21:41:17.001378Z

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

tolitius 2016-12-02T21:44:37.001380Z

it might have to do with: https://github.com/tolitius/mount/issues/50

tolitius 2016-12-02T21:44:58.001382Z

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

tolitius 2016-12-02T21:47:13.001383Z

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_ananev 2016-12-02T21:49:26.001388Z

yes

mike_ananev 2016-12-02T21:50:28.001389Z

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

tolitius 2016-12-02T21:54:07.001390Z

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

tolitius 2016-12-02T21:54:30.001391Z

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

mike_ananev 2016-12-02T21:56:53.001392Z

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

mike_ananev 2016-12-02T21:57:34.001394Z

I put intentionally 1/0

mike_ananev 2016-12-02T21:58:20.001395Z

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

mike_ananev 2016-12-02T22:00:02.001396Z

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

tolitius 2016-12-02T22:00:22.001397Z

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_ananev 2016-12-02T22:00:55.001399Z

hmmm, I will check right now ones more

tolitius 2016-12-02T22:03:04.001400Z

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_ananev 2016-12-02T22:04:17.001401Z

no such var mount/running-states

mike_ananev 2016-12-02T22:04:42.001402Z

is it in 0.1.11 ?

tolitius 2016-12-02T22:05:16.001403Z

ah.. it could be yes

tolitius 2016-12-02T22:05:37.001404Z

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

tolitius 2016-12-02T22:06:00.001405Z

0.1.11-SNAPSHOT

mike_ananev 2016-12-02T22:10:28.001406Z

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

mike_ananev 2016-12-02T22:11:44.001407Z

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?

mike_ananev 2016-12-02T22:14:10.001409Z

I mean

tolitius 2016-12-02T22:35:57.001413Z

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

mike_ananev 2016-12-02T22:42:41.001414Z

@tolitius got it! thanx!

tolitius 2016-12-02T22:57:00.001415Z

sure, good luck 🙂