Can an Elixir OTP Supervisor just "use Supervisor" or does it have to "use Genserver"?

Nona Source

So looking at examples, I'm a bit confused:

The elixir docs ( seem to suggest you can build supervision trees with just a "use Supervisor" macro.

But all the examples I see in blog posts / random Internet searches "use Genserver". So does a supervision tree have to use the Genserver interface?

I think I need some pointers in the right direction, maybe code to look at or a clearer example.



answered 10 months ago Steve Pallen #1

Designing the supervision tree in a complex application can be one of the most challenging aspect of architecting an opt application. However, for the most part, I find that most applications done need that complexity.

First, so mix the concept of supervisors with the processes they supervise. i.e. GenServers, Agents, Tasks, GenFSMs, etc.

For many simple apps/web apps, a flat supervisor tree will work. The work flow goes like this;

  • Add a new GenServer module
  • Add the startup of that module to your main supervisor worker(NewServer, [])

However, for more complicated solutions you may want to finer control of which servers are linked when one fails. This is where you would introduce another layer to the supervisor tree. In this case, you would add a new supervisor module and start it like

# application
children = [
  worker(WorkerMod1, args),
  worker(WorkerMod2, args2),
  supervisor(NextLevelSup, sargs)
# ....

# next_level_sup
children = [
  worker(GenServer1Grp1, args, id: "some unique id", restart: :temporary),
  worker(GenServer2Grp2, args2, id: "id2", restart: :transient),
# ...

Then choose the restart strategy you want to use for the new supervisor.

tl;dr Each server goes in a module. Each server is started from a supervisor in a different module.

One of my Elixir apps (the first one I designed) Has a number of levels of supervision. You can find more information on this video TS 12:10

comments powered by Disqus