Dynamic Supervisor Spec in Elixir

Viewed 1198

I have created a GameSupervisor supervision module that I use to dynamically create a child of instance GameServer (GenServer). I can see GameServer's start_link method is invoked when GameSupervisor.start function is called but it doesn't keep the pid alive. Process.alive?(pid) in iex always return false if the restart strategy is set to temporary. If I set the restart to transient or permanent it ends up calling GameServer.start_link again when I call GenServer.cast on that pid.

Is invoking start_child not automatically add the pid to the supervision tree and keep it alive?

GameSupervisor.ex

defmodule Prest.GameSupervisor do

  alias Prest.GameServer
  use Supervisor

  @name :game_sup

  def start_link() do
    IO.puts "start link"
    Supervisor.start_link(__MODULE__, [], [name: @name])
  end

  def start(uid) do
    IO.puts "start bucket"
    {:ok, child} = Supervisor.start_child(@name, [uid])
  end

  def init([]) do
    IO.puts "init sup"
    children = [
      worker(GameServer, [], restart: :transient)
    ]

    supervise(children, strategy: :simple_one_for_one)
  end

end

GameServer.ex

  defmodule Prest.GameServer do
  use GenServer

  # Client API

  def start_link(uid) do
    IO.puts "start game server"
    GenServer.start_link(__MODULE__, uid, [])
  end

  def post(pid, event_id) do
    :gen_server.cast(pid, {:event, event_id})
  end

  # Server API

  def init(uid) do
    {:ok, {uid, [], []}}
  end

  def handle_cast({:event, event_id}, state) do
    #state = [event_id|state]
    {:noreply, "ok", state}
  end
end

Thanks

1 Answers
Related