I’ve been following the book Programming Elixir and there’s this GenServer below as part of an app:
defmodule Duper.Gatherer do
use GenServer
@me Gatherer
def start_link(worker_count) do
GenServer.start_link(__MODULE__, worker_count, name: @me)
end
.
.
end
So our GenServer
’s name is Gatherer
.
And then how we add this GenServer to our supervision tree is:
def start(_type, _args) do
children = [
{Duper.Gatherer, 50}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_all, name: Duper.Supervisor]
Supervisor.start_link(children, opts)
end
So if we check the docs for start_link/3
it says,
Starts a
GenServer
process linked to the current process.
What I couldn’t get is if our GenServer
is called Gatherer
why do we add the module name to the children list instead of the GenServer name?
Another thing is when I checked the source code for GenServer.start_link/3
I see this called
do_start(:link, module, init_arg, options)
I don’t really know what link
there really means but it reminds me of Process.link/1
so if we link two processes then they’re informed about each other’s exits, right? So how does Supervisor
handle the exit of child apps so that it doesn’t crash when a child app exits?
Sorry if my questions are stupid, as it’s quite late here and my brain can’t process things anymore. I just didn’t want to forget the questions, so I’ll take a look at my questions tomorrow again and edit if necessary.