Powered by AppSignal & Oban Pro

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 SupervisorsDominoes

Supervised Stack

Previously we created a Stack GenServer process.

We’ve made a slight modification to the Stack process by adding a start_link/1 function so this named Stack can be started under a Supervisor.

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.

{:ok, stack_pid} = GenServer.start_link(Stack, [])

GenServer.call(stack_pid, {:push, 1}) |> IO.inspect(label: "push")
GenServer.call(stack_pid, {:push, 2}) |> IO.inspect(label: "push")
GenServer.call(stack_pid, {:push, 3}) |> IO.inspect(label: "push")

GenServer.call(stack_pid, :pop) |> IO.inspect(label: "pop")
GenServer.call(stack_pid, :pop) |> IO.inspect(label: "pop")
GenServer.call(stack_pid, :pop) |> IO.inspect(label: "pop")

# The Final State Is Empty.
:sys.get_state(stack_pid)

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(stack_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 so that it will be restarted when it crashes. In the Elixir cell below, start the Stack process under a supervisor so that it will restart with an empty stack when it crashes.

Example Solution

children = [
  {Stack, []}
]

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

Keep in mind, if you have already started a supervisor with the Stack process, your livebook may crash. You can resolve this issue by simply re-running the cell below to start the supervisor again.

You should be able to send a :pop message to the Stack process and the Supervisor will restart the Stack process.

Uncomment and evaluate the code below to test your supervisor.

# 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 SupervisorsDominoes