jonnifyelixir
F3 · The Elixir Language / F3.09 · playground

F3 · OTP · capstone lab

The process playground

A live sandbox for everything in this chapter. Send messages into a worker's mailbox and drain them through its receive loop; watch state accumulate; issue a synchronous call and read the reply; then crash a worker and watch the supervisor restart it under the strategy you pick. The whole actor model, in one board you drive.

This is a model of the BEAM's behaviour, not a running node, but it follows the same rules: messages queue in a mailbox, a process holds state between them, a crash stays inside one worker, and a supervisor decides who restarts. Each worker carries a branded PRC Snowflake PID, minted fresh on every restart.

The playground

Pick a focused worker, then send it :inc or :get — the messages land in its mailbox. Process them one at a time or drain the lot, and the state moves. Choose a restart strategy, then crash the focused worker and read each sibling's fate in the tree and the log.

Live supervision tree · drive it yourself

Supervisor :one_for_one Tally counts :inc PRC— STATE 0 alive r 0 Notifier counts notices PRC— STATE 0 alive r 0 Cache counts entries PRC— STATE 0 alive r 0
Focused worker
Restart strategy
Mailbox · Tally · depth 0
(empty)
Last call reply
—
Event log · newest first

Everything you see follows from three rules: a message waits in the mailbox until the process reads it, the state is whatever the last message left behind, and a crashed worker comes back clean while the strategy decides who comes back with it.

What you are driving

Each worker is a GenServer (F3.08) holding a count. A send :inc is a cast that lands in the mailbox; processing it runs handle_cast and returns the next state. A send :get followed by processing is a call that runs handle_call and replies. The supervisor and its strategy are the same ones from the supervisors lesson — crashing a worker is the model's stand-in for "let it crash".

defmodule Portal.Worker do
  use GenServer
  def init(n), do: {:ok, n}
  def handle_call(:get, _from, c), do: {:reply, c, c}
  def handle_cast(:inc, c), do: {:noreply, c + 1}
end

children = [Portal.Tally, Portal.Notifier, Portal.Cache]
Supervisor.init(children, strategy: :one_for_one)

F3.07 & F3.08 · the parts

Spawn, mailbox, receive, the state loop, the GenServer behaviour, call and cast, supervisors and strategies.

F3.09 · the whole running system

One supervised tree you drive end to end — messages in, state held, a crash absorbed and restarted.

This is the final module of F3 — The Elixir Language. Next chapter: F4 — Algorithms & Data Structures, where recursion meets lists, trees, and big-O on the BEAM.

References

Primary sources for this lab, and where it connects in the course.

Sources

Related in this course