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

Dominoes

exercises/deprecated_dominoes.livemd

Dominoes

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 Supervised StackMonster Spawner

Dominoe

Dominoes are square tiles often placed near each other to make satisfying effects when it falls and knocks each other over.

We’ve created a named Domino process that can receive a generic :fall message to cause it to crash. It will also print a message every time it’s started with the name of the process.

defmodule Domino do
  use GenServer

  def start_link(opts) do
    IO.inspect(opts, label: "STARTED DOMINO")
    GenServer.start_link(__MODULE__, opts, name: opts[:name])
  end

  @impl true
  def init(opts) do
    {:ok, opts}
  end

  @impl true
  def handle_info(:fall, _state) do
    raise "error"
  end
end

You’re going to start three Domino processes under a supervisor. When one crashes, they should restart sequentially in the order they are defined.

flowchart TD 
    Supervisor
    Supervisor --> Domino1
    Supervisor --> Domino2
    Supervisor --> Domino3

    classDef crashed fill:#fe8888;
    classDef terminated fill:#fbab04;
    classDef restarted stroke:#0cac08,stroke-width:4px

    class Domino2 crashed
    class Domino3 terminated
    class Domino2,Domino3 restarted

For example, if Domino2 crashes, then Domino3 should also be restarted. If Domino3 crashes, no other processes are affected.

Hint Use the :rest_for_one strategy for your supervisor.

Example Solution

children = [
  %{
    id: :domino1,
    start: {Domino, :start_link, [[name: :domino1]]}
  },
  %{
    id: :domino2,
    start: {Domino, :start_link, [[name: :domino2]]}
  },
  %{
    id: :domino3,
    start: {Domino, :start_link, [[name: :domino3]]}
  }
]

Supervisor.start_link(children, strategy: :rest_for_one)

Keep in mind, if you have already started a named process, the supervisor might crash when you attempt to start it again. Re-evaluate the cell after the livebook crashes to resolve this issue.

Send your dominos messages to ensure they are crashing in the correct order. They will log a message that demonstrates the Domino.start_link/1 function was called again.

Example Solution

Replace :domino_name with the name of a domino process you started in the supervisor above.

Process.send(:domino_name, :fall, [])

Test sending each Domino process a message individually.

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 Dominoes 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 Supervised StackMonster Spawner