How to implement Supervisors of Supervisors?

tl;dr:
I simply want to build a Supervision tree like in this picture:

But I can’t find any information on how doing so. What I did so far:
In appplication.ex

defmodule MyApp.Application do
    use Application
    def start(_type, _args) do
        children = [{MyApp.Supervisor, [:hello]}]
        opts = [strategy: :one_for_one]
        Supervisor.start_link(children, opts)
    end
end

In supervisor.ex

defmodule MyApp.Supervisor do
    use Supervisor
    def start_link(args) do
        children = [{MyApp.Worker, [:world]}]
        opts = [strategy: :one_for_one]
        Supervisor.start_link(children, opts)
    end
end

The Worker is a typical :pop :push Stacklike implementation.
When I try to run it, I get the following error:

** (ArgumentError) The module MyApp.Supervisor was given as a 
child to a supervisor but it does not impement child_spec/1
[...]

So I tried to implement the child_spec function, though in the Supervisor doc it is written, that the use Supervisor will do that for me.

I honestly don’t know how to build such a simple supervision tree. I have the PragProg book “Programming Elixir” which handles exactly this situation, but it is written for Elixir 1.3 and I want to implement it with 1.5. The Supervisor.Spec documentation tells me that it is deprecated, but that is the only way I find on the internet, where such a simple Supervision tree is built (with the supervise() function.

Am I misunderstanding basic idioms here? Are you supposed not to implement such a structure with maybe even more levels of Supervisors?

thanks in advance so far.

Are you not missing the init/1 callback?

See https://elixir-lang.org/getting-started/mix-otp/supervisor-and-application.html

defmodule KV.Supervisor do
  use Supervisor

  def start_link(opts) do
    Supervisor.start_link(__MODULE__, :ok, opts)
  end

  def init(:ok) do
    children = [
      KV.Registry
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end
end
1 Like

Okay, I got it now. Thanks @orestis!

I thought I already tried that, because I did read that article already a couple of times. Might have messed up something.