Understanding Elixir Supervisor
This and the other “Deck” posts are a repurposing of flashcard study decks to Q&A blog posts.
What is the function of an Elixir Supervisor?
It is a process that monitors other processes and restarts them based on a specified supervision strategy.How are supervisors related to the Elixir axiom "let it crash"?
Supervisors will spin up new processes in the event of a failure. If a process errors/fails a new one will be started without the developer having to step in and restart anything. Aka they can "let it crash".How are processes under supervision related to the Supervising process?
They are considered child processes of the supervisor.How do you start a supervisor that monitors child processes?
You pass a list of child processes to the supervisor. Each child process is a `Map` that contains keys of `id` and `start`. The `id` is the process and `start` is a `Tuple` containing information on how to run the process. Example below:children = [ %{ id: ChildProcess, start: {ChildProcess, :start_link, [[:initial_state]]} } ]
{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)