Fork me on GitHub
#component
<
2016-04-14
>
keeth21:04:33

hello, I have a system which contains two subsystems as children.. i’d ideally like to pass a common ‘database’ component to both subsystems, which those subsystems will pass into the components they contain.. is there a recommended way to handle that?

sveri21:04:59

@keeth: Not sure if I understand your question correctly, but I pass my db like this: https://github.com/sveri/closp/blob/master/resources/leiningen/new/closp/clj/components/components.clj you can see the config component passed to two different components. AFAIU you can compose them as you want.

keeth21:04:43

@sveri: thanks the main thing i am trying to avoid is the component being started and stopped more than once

keeth21:04:54

i think i have a solution, producing a System implementation that allows to exclude certain components from being started and stopped by the system

keeth21:04:59

thankfully component/(start|stop)-system lets you override the keys to be stopped/started, so i don’t have to re-implement too much of system

sveri21:04:03

@keeth: Yea, the thing is, if you cannot restart a component n times, it is not a component anymore (thats my understanding, others may see this different). So you need to find a different solution to it. Out of interest, what is it that cannot be restarted?

keeth21:04:57

it’s not that it can’t be restarted, but if I pass it to two different subsystems as dependencies, (.start db) will be called twice when the app starts up

sveri21:04:13

I dont grok fully yet what you are doing. But a component, should only be started once, no matter to how many other components you pass it to. Maybe I dont understand what subsystemis in your case?

keeth21:04:30

i have a top-level system whose system map contains two child systems

keeth21:04:46

the top-level system is also responsible for starting/stopping the db component

keeth21:04:16

so at app start, the top-level/parent system should start the db, and inject it into the child systems, and start the child systems after starting the db

keeth21:04:29

but even tho the child systems will have a :database key in their system maps, i don’t want them to try starting it

sveri21:04:06

Hm...so you have three systems?

keeth21:04:19

yep, one parent with two children

sveri21:04:56

Interesting, I never tried that. And the db component is started three times?

keeth21:04:04

currently yes

keeth21:04:13

but i think i almost have it fixed

sveri21:04:52

I am not sure that component supports that, would be interesting to hear what @stuartsierra has to say about it.

sveri21:04:56

How did you fix it?

sveri21:04:03

Conditional start / stop?

keeth21:04:10

see snippet above, i’m made a thin system implementation that allows me to exclude a set of keys of components which i do not want the system to start or stop

sveri21:04:10

Ah ok, you use that one

keeth21:04:41

usage is something like:

(system/system-map-excluding
      {:component1 (component/using (new-component-1) [:database])}
      #{:database})

keeth21:04:20

anyway, will see in a moment if it works 😬

stuartsierra21:04:30

Nested systems are complicated and usually unnecessary; I recommend against them.

sveri21:04:52

Thats my feeling too, sounds like over architecturing

keeth21:04:07

thanks for your feedback guys.. ya I guess since systems implement the same protocol as components, I figured they could be composed together as if they were components