Current section
Files
Jump to
Current section
Files
lib/deep_supervisor.ex
defmodule Superintelligence.DeepSupervisor do
use Supervisor
def start_link(depth) when is_integer(depth) and depth >= 0 do
Supervisor.start_link(__MODULE__, depth, name: via_tuple(depth))
end
@impl true
def init(0) do
# At depth 0, we spawn a worker instead of another supervisor
children = [
{Superintelligence.Worker, []}
]
Supervisor.init(children, strategy: :one_for_one)
end
@impl true
def init(depth) when depth > 0 do
# Spawn a child supervisor at depth - 1
children = [
%{
id: :child_supervisor,
start: {Superintelligence.DeepSupervisor, :start_link, [depth - 1]},
type: :supervisor
}
]
Supervisor.init(children, strategy: :one_for_one)
end
defp via_tuple(depth) do
{:via, Registry, {Superintelligence.Registry, {:supervisor, depth}}}
end
end