[erlang-questions] non-trivial supervisor idioms?

Daniel Goertzen <>
Mon Sep 27 17:59:47 CEST 2010


On Mon, Sep 27, 2010 at 9:31 AM, Ulf Wiger
<>wrote:

> On 27/09/2010 16:15, Daniel Goertzen wrote:
>
>> I've read the documentation on supervision and have seen a few tutorials,
>> but they don't seem to move beyond the core concepts.  For example, what
>> happens if you want to check and optionally setup an mnesia schema during
>> startup...where should this code go?  In the supervisor init() or
>> start_link() function?  Should I have my supervisor create a worker
>> process
>> whole sole job is to do this kind of setup and then dynamically add other
>> workers (or supervisors) to the supervisor with start_child()?
>>
>
> I strongly recommend doing that sort of thing in a separate procedure,
> rather than in the startup phase.
>
> If you want your application to be able to bootstrap itself, I would
> suggest that you either:
>
> - create a special application that runs before your other apps,
>  and verifies that the installation is ok. To this end, it might be
>  useful to know that you can pre-sort the .rel file. The systools lib
>  will only change the sort order if needed to respect start
>  dependencies.
> - Introduce start_phases, then do minimal work in the init function,
>  and push the rest to functions that are called from start phase
>  hooks. This also has the advantage that you know that your processes
>  are all started and ready to respond during the init phase.
>
> Start phases are documented in
> http://www.erlang.org/doc/apps/kernel/application.html#Module:start_phase-3
>
> BR,
> Ulf W
>
>
Thanks, I forgot about start phases.  Makes perfect sense for mnesia init.

Now how about this scenario:  I am opening an ssh client connection which
will have multiple channels.  The channel processes require a connection
reference, so I have to establish the connection before creating them.  I
will keep these channel processes under a supervisor.  Now, I have the
choice of establishing the connection in supervisor init() and then
returning  childspecs with the connection reference built-in, or creating a
worker to establish the connection and then dynamically add channel
processes to the supervisor afterwards.

I have to think this kind of complex startup happens frequently enough that
best practices have been established.  Or perhaps these situations are
actually rare?

BTW, my gut feeling is that I should leave supervisor init() alone and have
a worker perform complex startup.

Thanks,
Dan.


More information about the erlang-questions mailing list