Erlang how to set up a supervisor tree application that is self contained

284 views Asked by At

I am not sure if I can properly articulate my question, but I will try as best I can.

I find it awkward the way you have to set up a supervisor tree(sup, sup-tress, and other more complex trees).

Lets take for example, the following (I have already made these examples and expanded on them): http://learnyousomeerlang.com/supervisors

I dont like the idea of starting my application(supervisor trees), and then having to send erl commands on the shell to get the application to behave the way I want it too. Take the bottom shell example from the site:

1> supervisor:start_child(band_supervisor, [djembe, good]).
Musician Janet Tennelli, playing the djembe entered the room
{ok,<0.690.0>}
2> supervisor:start_child(band_supervisor, [djembe, good]).
{error,{already_started,<0.690.0>}}
3> supervisor:start_child(band_supervisor, [drum, good]).
Musician Arnold Ramon, playing the drum entered the room
{ok,<0.696.0>}
3> supervisor:start_child(band_supervisor, [guitar, good]).
Musician Wanda Perlstein, playing the guitar entered the room
{ok,<0.698.0>}
4> supervisor:terminate_child(band_supervisor, djembe).
{error,simple_one_for_one}
5> musicians:stop(drum).
Arnold Ramon left the room (drum)
ok

To me it is weird I would have to start my supervisor(or whatever), and then further type in other commands via shell to get what I want.

For a real-world example, lets say for example I have a Listen Supervisor, and on this supervisor I say I want to always have 100 listen workers. Their jobs are to listen on the socket that Listen Supervisor created. Given the tutorial (and others online), I would have to type in the shell 100 times to start all children.

So, I guess my question is the following ...

How can this all be done within the application or supervisor(however you want to do it), so that nothing has to be typed into the shell after starting your beam file.

I know there has to be a way, but I just can not come up with a good enough architecture that allows me to do this is a standard and clean way.

Thanks for your time.

1

There are 1 answers

4
Pouriya On BEST ANSWER

You have some options.

You can define some variables in env of .app file or in sys.config file and get them in init/1 of supervisor and return enough number of childspecs.

If count of children and other info about them come from other code and they may come in different times, you can have a child named dispatcher in supervisor, Your dispatcher's duty is getting information about starting children and starting them as child of supervisor.