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

Supervised Stack

exercises/supervised_stack.livemd

Supervised Stack

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 Supervisor DrillsGames: Supervised Score Tracker

Supervised Stack

You’re going to create a stack project that starts a name Stack GenServer process under a supervisor.

mix new stack

Here’s an example Stack GenServer you can use.

defmodule Stack do
  use GenServer

  def start_link(opts) do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

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

  @impl true
  def handle_call(:pop, _from, state) do
    [head | tail] = state
    {:reply, head, tail}
  end

  @impl true
  def handle_call({:push, element}, _from, state) do
    new_state = [element | state]
    {:reply, new_state, new_state}
  end
end

We’re able to push and pop elements off of the stack. However, there’s a bug. If we try to pop/1 an item off of an empty stack, the process will crash due to a function clause error because the handle_call/2 function expects a list with one or more elements.

Uncomment the following code to watch the Stack crash.

# {:ok, pid} = GenServer.start_link(Stack, [])
# GenServer.call(pid, :pop)

Normally, we could handle this by using control flow. For example, we could make another handle_call/3 function clause for when the stack is empty.

# Return `nil` When The Stack Is Empty.
@impl true
def handle_call(:pop, _from, []) do
  {:reply, nil, []}
end

Instead, you’re going to start the Stack process under a supervisor in your application so that it will be restarted when it crashes.

Example Solution

children = [
  {Stack, []}
]

{:ok, supervisor_pid} = Supervisor.start_link(children, strategy: :one_for_one)

Crash The Stack

Open the IEx shell and send the Stack a :pop message to cause it to crash and restart.

$ iex -S mix
iex> GenServer.call(Stack, :pop)

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 Supervised Stack 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 Supervisor DrillsGames: Supervised Score Tracker