Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Supervisor Drills

exercises/supervisor_drills.livemd

Supervisor Drills

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue SupervisorsSupervised Stack

Drills

Drills help you develop familiarity and muscle memory with syntax through repeated exercises. Unlike usual problems, Drills are not intended to develop problem solving skills, they are purely for developing comfort and speed.

This set of drills is for Supervisors.

> A supervisor is a process which supervises other processes, which we refer to as child processes. Supervisors are used to build a hierarchical process structure called a supervision tree. Supervision trees provide fault-tolerance and encapsulate how our applications start and shutdown. > > * HexDocs: Supervisor

Supervisor

We’ve created a Worker GenServer for you to use with the following Supervisor drills.

defmodule Worker do
  use GenServer

  def start_link(state, opts \\ []) do
    GenServer.start_link(__MODULE__, state, opts)
  end

  def init(state) do
    {:ok, state}
  end
end

Use Supervisor.start_link/3 to start a supervisor process with no children. Use Supervisor.which_children/1 to see the empty list of children.

Example Solution

We often bind children to a variable in demonstrations.

children = []

Supervisor.start_link(children, strategy: :one_for_one)

But that’s not enforced, just idiomatic.

Supervisor.start_link([], strategy: :one_for_one)

Use Supervisor.start_link/3 function to start a supervisor with the Worker as a child using the map syntax.

Example Solution

children = [
  %{
    id: :worker1,
    start: {Worker, :start_link, ["initial state"]}
  } 
]

Supervisor.start_link(children, strategy: :one_for_one)

Use Supervisor.start_link/3 function to start a supervisor three Worker children using the map syntax.

Example Solution

children = [
  %{
    id: :worker1,
    start: {Worker, :start_link, ["initial state"]}
  },
  %{
    id: :worker2,
    start: {Worker, :start_link, ["initial state"]}
  },
  %{
    id: :worker3,
    start: {Worker, :start_link, ["initial state"]}
  }
]

Supervisor.start_link(children, strategy: :one_for_one)

Use Supervisor.start_link/3 function to start a supervisor with the Worker as a child using the tuple syntax.

Example Solution

children = [
  {Worker, "initial state"}
]

Supervisor.start_link(children, strategy: :one_for_one)

Use Supervisor.start_link/3 function to start a supervisor with the Worker as a child using the map syntax to start the Worker process as a named process.

Example Solution

children = [
  %{
    id: :worker1,
    start: {Worker, :start_link, ["initial state", [name: :named_worker1]]}
  }
]

Supervisor.start_link(children, strategy: :one_for_one)

Usually worker GenServers should define a start_link/1 function to start under a supervision tree. However, this is idiomatic and we can (but usually shouldn’t) break this rule.

Start the following NonIdiomatic GenServer under a supervision tree using the starter/3 function.

Example Solution

children = [
  %{
    id: :never_do_this,
    start: {NonIdiomatic, :starter, ["arg", "arg", "arg"]}
  }
]

Supervisor.start_link(children, strategy: :one_for_one)
defmodule NonIdiomatic do
  use GenServer

  def starter(_arg1, _arg2, _arg3) do
    GenServer.start_link(__MODULE__, [])
  end

  def init(_opts) do
    {:ok, "initial state"}
  end
end

Use Supervisor.start_link/3 function to start a supervisor with three Worker children using the tuple syntax.

This should cause the supervisor to crash because each Worker has the same id.

Comment your solution when finished to avoid crashing this livebook.

Example Solution

children = [
  {Worker, "initial state"},
  {Worker, "initial state"},
  {Worker, "initial state"}
]

Supervisor.start_link(children, strategy: :one_for_one)

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Supervisor Drills exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue SupervisorsSupervised Stack