Question

Is it possible to cascade supervisors inside an application?

E.g. supervisor sup1 spawning a child process which creates a supervisor sup2 ?

Was it helpful?

Solution

You probably want to add the child as supervisor.

It is an entry in the childspec of a child of a supervisor. The "type" of the child can be set to "supervisor":

http://www.erlang.org/doc/design_principles/sup_princ.html#spec

Probably you can do it via the child starting a supervisor itself too but it is at least less elegant and it is less evident what you are doing.

HTH, h.

OTHER TIPS

Yes, you can simply add supervisors as children of a supervisor. Or mix and match. I do something like this usually:

(in my top level supervisor)

init([]) ->
    Args = [],
    ModuleArray = [get_info(Module, Args)
           || Module
              <- [emx_nodestate, emx_sup_data, emx_sup_util, emx_sup_api,
                  emx_flow]],
    {ok, {{one_for_one, 3, 1}, ModuleArray}}.

get_info(Module, Args) ->
    {Module, {Module, start_link, [Args]}, permanent, 10000,
     worker, [Module]}.

And then something like emx_sup_data (behaviour supervisor) contains:

init([]) ->
    Args = [],
    ModuleArray = [get_info(Module, Args)
           || Module <- [job_housekeep]],
    {ok, {{one_for_all, 3, 1}, ModuleArray}}.

get_info(Module, Args) ->
    {Module, {Module, start_link, [Args]}, permanent, 10000,
     worker, [Module]}.

and something like emx_nodestate (behaviour gen_server)

init([]) ->
    {ok, #state{status=starting, interested=[]}}.

Works like a dream...!

To see how other folks structure their apps why dont you fire up a shell and run the toolbar:

toolbar:start()

That gives you a graphical view of a supervisor heirarchy. A quick look at the kernel supervisor tree, or mnesia or yaws, will show you what a 'normal' supervisor tree looks like.

You compose an application of sub-systems supervised by an application supervisor. Each sub-system can be many sub-sub-systems under the sub-system supervisor (continue applying pattern in recursive functional manner until you run out of granularity...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top