[erlang-questions] supervisor started from gen_server

Mazen Harake mazen.harake@REDACTED
Fri May 20 11:25:27 CEST 2011


I don't agree with the "never" part. There are corner cases and I would, in
general, absolutely agree with what you wrote but here is one example of an
exception.

In my IRC library (https://github.com/mazenharake/eirc) I have the following
scenario:

One can create 1..n clients. Each client is a process (running a gen_server
in eirc_cl.erl). A client can have many processes associated with it, in my
case these are called Bots. Each bot is started and supervised by a client.
A bot shall not terminate unless told to do so (gracefully) or if the parent
dies (i.e the client). Now if I manually start a supervisor instance inside
the gen_server I can simply make use of the supervisor functionality without
handling any of that in my gen_server. I can add and remove children (bots)
dynamically from my supervisor I.e. I don't have to maintain my own list of
them and when I (the client) exits my children (I.e. the supervisor) will
exit and thus my bots will exit... all according to OTP principals and I
don't need to catch exit signals. This makes it REALLY easy to clean up
things since all I have to do is to close the client process, the client
process doesn't even need logic in the terminate callback since the children
will exit with the same reason. If my supervisor was "on the outside" (like
you suggest and which it normally is) I would have to make sure to clean up
after myself with the supervisor.

So it is much simpler (from an isolation point of view) to (in my case)
start a supervisor Inside the gen_server.

never say never ;)

/M




On 18 May 2011 14:13, Vance Shipley <vances@REDACTED> wrote:

> On Wed, May 18, 2011 at 11:38:50AM +0200, Roberto Ostinelli wrote:
> }  just a quick question: can you think of any case in which starting a
> }  supervisor from a gen_server would actually be a perfectly fit idea?
>
> Never.
>
>                      +---------+
>                      | foo_sup |
>                      +-+-----+-+
>                       /       \
>           +----------+--+   +--+---------+
>           | bar_sup_sup |   | foo_server |
>           +---+---------+   +------------+
>               |
>          +----+----+
>          | bar_sup |
>          +---------+
>
> Start a supervisor for the new dynamic supervisor(s) and use
> supervisor:start_child/2 from the gen_server to start it.
>
> --
>         -Vance
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110520/2e7cf6e8/attachment.htm>


More information about the erlang-questions mailing list