In my Erlang/OTP application I have a one_for_all
supervisor (sup) with several childs. One of the children (child1 with gen_server
behaviour) should be able to send messages to the other one (child2 with supervisor
behaviour). Of course, I can register it but clogging up the global scope with excess names doesn't seem to be a good idea.
Thereby the only way of making such interaction possible is to provide child1 with the pid of child2. No sooner said than done. There is supervisor:wich_children/1
call with appropriate functionality. Just passing sup's pid as the argument to chidl1, calling which_children
in the child1:init
, and... getting a deadlock. sup is waiting child1 to start, child1 is waiting sup for children descriptions:
init(SupPid) ->
Descriptions = supervisor:which_children(SupPid),
... .
This can be fixed by the following kludge:
init(SupPid) ->
gen_server:cast(self(), initialize),
... .
handle_cast(initialize, State) ->
Descriptions = supervisor:which_children(SupPid),
... % Generating new state containing desired pid
{noreply, NewState}.
However, I wasn't satisfied by this solution.
The question is: What is the most conventional way of interacting between supervision tree members according to OTP design principles?