If my supervised init
callback wants to fail, it can return {:stop, reason}
, but that seems to stop by supervisor as well.
Is that because of this :
If the start function of any of the child processes fails or returns an error tuple or an erroneous value, the supervisor first terminates with reason :shutdown all the child processes that have already been started, and then terminates itself and returns {:error, {:shutdown, reason}}.
https://hexdocs.pm/elixir/Supervisor.html#start_link/2
If so, is there a way to exit from the init
to stop the supervised child, and keep the main supervisor alive?
Example code :
defmodule App.Supervisor do
use Supervisor
def start_link() do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
children = [
supervisor(App.ChildSupervisor, [])
]
supervise(children, strategy: :one_for_one)
end
end
defmodule App.ChildSupervisor do
use Supervisor
def init do
{:stop, "reason"}
end
end