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?
@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.
@sveri: thanks the main thing i am trying to avoid is the component being started and stopped more than once
i think i have a solution, producing a System implementation that allows to exclude certain components from being started and stopped by the system
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
@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?
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
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 subsystem
is in your case?
i have a top-level system whose system map contains two child systems
the top-level system is also responsible for starting/stopping the db component
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
but even tho the child systems will have a :database key in their system maps, i don’t want them to try starting it
Hm...so you have three systems?
yep, one parent with two children
Interesting, I never tried that. And the db component is started three times?
currently yes
but i think i almost have it fixed
I am not sure that component supports that, would be interesting to hear what @stuartsierra has to say about it.
How did you fix it?
Conditional start / stop?
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
Ah ok, you use that one
usage is something like:
(system/system-map-excluding
{:component1 (component/using (new-component-1) [:database])}
#{:database})
anyway, will see in a moment if it works 😬
Nested systems are complicated and usually unnecessary; I recommend against them.
Thats my feeling too, sounds like over architecturing
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