[erlang-questions] Question about behaviours in Erlang/OTP

zxq9 zxq9@REDACTED
Mon Jul 14 03:48:07 CEST 2014

On Sunday 13 July 2014 19:59:36 Yves S. Garret wrote:
> Hello,
> I'm looking at some code that was written where a user uses a behaviour of
> gen_server and then uses a process_flag(trap_exit, true).  To me, this
> seems like a supervisor behaviour would work better.
> What would be a better approach in this case?  To declare this module a
> supervisor?  Is it possible (sensible? logical?) to give the same module
> gen_server and supervisor behaviours?

I'm certainly no expert at designing supervisor trees, but the thing I usually 
see is more layers of supervision, each with a distinct type of responsibility 
(hence "tree" and not master/slave). So a top-level supervisor that can trap 
exits (and might do other top-level stuff), which supervises another 
supervisor which monitors whatever work is going on, and working processes 
like gen_servers at the bottom. This can get pretty wide and deep depending on 
the application.

In other words, a layer of supervision for each conceptual level of 
supervisory tasks. Its a bit more indirection/layering than you might expect 
coming from another language, but this seems to be the way OTP expects systems 
to be built.

OTOH, if the program is not a permanent service -- like a program designed to 
be run from a command line, do its job, and terminate when it completes -- 
then it doesn't really matter. In this case even using gen_server might have 
been overkill compared to writing the code in a more direct manner. The whole 
issue of supervision hinges on the nature of the program, and not every 
program benefits from this, as many programs are not long-running services.

More information about the erlang-questions mailing list