Just realized that start_link
(or whatever is passed via start
in the child_spec
) not only can return {:ok, pid}
, but also {:ok, pid, info}
, where info
can be any term and is ignored by supervisors - see Supervisor — Elixir v1.13.4. So, I got an idea that this info
can be used to return potentially useful information, like the PID of the worker that should be interacted with. So if one would like to use PID for interaction, they can do
{:ok, _supervisor_pid, worker_pid} = MyGenServer.start_link()
GenServer.call(worker_pid, :ping)
While using under a supervisor, the worker_pid
is not accessible, enforcing communication via names and so on.
Consider though that if the supervisor restarts the worker, the worker_pid
would become obsolete in your last example.
The idea is that the supervisor actually doesn’t restart anything - it’s only there to make sure that all the processes terminate properly (see the first post). If restarts are needed, the user has to put the entire subtree under another supervisor.
How do you enforce the guarantee that the supervisor which you are starting at the top of the process group with MyGenServer.start_link()
will not restart any of its children unless it’s under another supervision tree?
1 Like
I see two options:
- setting
max_restarts
to 0
- creating the supervisor from scratch - it shouldn’t be that hard, since no restarting has to be done
actually that’s not really what I meant - the supervisor won’t restart children in any case, but if it’s under another supervisor, it’s going to be restarted itself
1 Like
ah yes, I misunderstood before your clarification, makes sense
1 Like