Child Specs in Elixir
Child specs are often confusing for people trying to convert the old Supervisor.Spec
based syntax to the newer child spec based syntax, people trying to integrate with erlang libraries or because they added more parameters to their start_link
functions and now wonder why it fails when trying to supervise the process.
Child spec map
To start, it’s important to know what a child spec is. It’s a map of data, which is used by supervisors to determine how they should start and interact with a certain supervised child. The format is documented in the Supervisor
docs for elixir as well as the :supervisor
docs for erlang, therefore I’ll keep my explanations short. There are 6 keys: :id
, :start
, :restart
, :shutdown
, :type
and :modules
. Besides :id
and :start
the keys are optional with defaults.
The child spec for an run-of-the-mill GenServer
essentially looks like this:
%{
id: Stack,
...