Skip to content

Instantly share code, notes, and snippets.

@JesseStorms
Last active January 9, 2023 23:12
Show Gist options
  • Save JesseStorms/13954fcbdda7b04b2767bd14236afd0c to your computer and use it in GitHub Desktop.
Save JesseStorms/13954fcbdda7b04b2767bd14236afd0c to your computer and use it in GitHub Desktop.
Shows you ways to make supervisors in supervisors

Supervisor tomfoolery

Mix.install([
  {:kino, "~> 0.8.0"},
  {:kino_vega_lite, "~> 0.1.7"}
])

Supervisor setup

Run in Livebook

This book is just showing and visualizing how trees look!

supervisor = Demo

children = [
  {DynamicSupervisor, strategy: :one_for_one, name: supervisor}
]

sid =
  case DynamicSupervisor.start_link(name: supervisor, strategy: :one_for_one) do
    {:ok, x} -> x
    {_, {_, x}} -> x
  end

Adding SubProcesses

{:ok, agent1} = DynamicSupervisor.start_child(Demo, {Agent, fn -> %{} end})
Agent.update(agent1, &Map.put(&1, :key, "value"))
Agent.get(agent1, & &1)
# => %{key: "value"}

{:ok, agent2} = DynamicSupervisor.start_child(Demo, {Agent, fn -> %{} end})
Agent.get(agent2, & &1)

DynamicSupervisor.count_children(Demo)
Kino.Process.render_sup_tree(sid)

Adding Supervisors

This adds a supervisor as a child, containing a process

{:ok, subid} = DynamicSupervisor.start_child(Demo, DynamicSupervisor)
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
Kino.Process.render_sup_tree(sid)
{:ok, subid} = DynamicSupervisor.start_child(subid, DynamicSupervisor)
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
{:ok, piid} = DynamicSupervisor.start_child(subid, DynamicSupervisor)
DynamicSupervisor.start_child(piid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(piid, {Agent, fn -> %{} end})
Kino.Process.render_sup_tree(sid)

Killing a supervisor

{:ok, tid} =
  DynamicSupervisor.start_child(
    piid,
    {DynamicSupervisor, strategy: :one_for_one, name: Carly_Who_We_Will_Literally_Kill}
  )

DynamicSupervisor.start_child(tid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(tid, {Agent, fn -> %{} end})
{:ok, subid} = DynamicSupervisor.start_child(tid, DynamicSupervisor)
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
{:ok, subid} = DynamicSupervisor.start_child(tid, DynamicSupervisor)
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
{:ok, subid} = DynamicSupervisor.start_child(subid, DynamicSupervisor)
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})
DynamicSupervisor.start_child(subid, {Agent, fn -> %{} end})

DynamicSupervisor.start_child(tid, {Agent, fn -> %{} end})
Kino.Process.render_sup_tree(sid)
# killing here, goodbye carly
DynamicSupervisor.terminate_child(piid, tid)
Kino.Process.render_sup_tree(sid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment