Skip to content

Commit

Permalink
add game supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
jrowah committed Nov 4, 2024
1 parent 57e84ee commit 7971e9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/battle_ship/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule BattleShip.Application do
children = [
# Starts a worker by calling: BattleShip.Worker.start_link(arg)
# {BattleShip.Worker, arg}
{Registry, keys: :unique, name: Registry.Game}
{Registry, keys: :unique, name: Registry.Game},
{BattleShip.GameSupervisor, []}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
23 changes: 23 additions & 0 deletions lib/battle_ship/game_supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule BattleShip.GameSupervisor do
@moduledoc false
use Supervisor

alias __MODULE__
alias BattleShip.Game

def start_link(_options), do: Supervisor.start_link(GameSupervisor, :ok, name: GameSupervisor)

def init(:ok), do: Supervisor.init([Game], strategy: :simple_one_for_one)

def start_game(name), do: Supervisor.start_child(GameSupervisor, [name])

def stop_game(name) do
Supervisor.terminate_child(GameSupervisor, pid_from_name(name))
end

defp pid_from_name(name) do
name
|> Game.via_tuple()
|> GenServer.whereis()
end
end

0 comments on commit 7971e9d

Please sign in to comment.