sasa1977 / elixir-in-action

Code samples for Elixir in Action book
947 stars 199 forks source link

proper_registry_supervisor why have init function? #13

Closed nhooyr closed 4 years ago

nhooyr commented 7 years ago

https://github.com/sasa1977/elixir-in-action/blob/c6c0813b1e463fe1211a0a5d998b3528752bb7b4/code_samples/ch09/proper_registry_supervision/lib/todo/system_supervisor.ex#L14

Why not directly use Todo.Supervisor?

sasa1977 commented 7 years ago

I presume you're asking why not use Supervisor.start_link/2, and avoid defining callbacks, right?

We could indeed simplify this module as:

defmodule Todo.SystemSupervisor do
  def start_link do
    import Supervisor.Spec

    Supervisor.start_link(
      [
        supervisor(Todo.Database, ["./persist/"]),
        supervisor(Todo.ServerSupervisor, []),
        worker(Todo.Cache, [])
      ],
      strategy: :one_for_one
    )
  end
end

And this is how I'd usually do it myself.

The main (well, the only) reason I didn't reach for that in the book is because I was worried people will be confused with this different form. It was more important for me to focus on how to organize supervision trees, than to show every aspect of the Supervisor API.

That said, I will consider discussing this approach in the next edition.

nhooyr commented 4 years ago

Going to close as it has been a long time and I'm not sure if this is relevant anymore.