Dmitry Krakosevich
Dmitry Krakosevich

Reputation: 305

Why is Process ID returning as undefined?

I have Supervisor and its child Genserver with code below:

defmodule MyApp.StatisticSupervisor do
  use DynamicSupervisor

  def start_link(_) do
    DynamicSupervisor.start_link(__MODULE__, [], name: {:global, __MODULE__})
  end

  def start_statistic(team_id) do
    DynamicSupervisor.start_child(global_name(), {MyApp.Statistic.Server, [team_id]})
  end

  def init(_) do
    DynamicSupervisor.init(strategy: :one_for_one)
  end
end

defmodule MyApp.Statistic.Server do
  def child_spec(team_id) do
    %{
      id: team_id,
      start: {__MODULE__, :start_link, [team_id]},
      restart: :transient
    }
  end

 def start_link(team_id) do
    GenServer.start_link(__MODULE__, %{team: team_id}, name: via_tuple(team_id))
  end

  def init(state) do
    {:ok, state}
  end

 defp via_tuple(team_id) do
    {:via, :syn, {:team, team_id}}
  end
end

Code is correct. Then I start genserver with command:

MyApp.StatisticSupervisor.start_statistic(1)
>
{:ok, #PID<0.973.0>}

Then, I'm executing code

DynamicSupervisor.which_children({:global, MyApp.StatisticSupervisor})

And command is returning:

[
  {:undefined, #PID<0.973.0>, :worker, [MyApp.Statistic.Server]}
]

I'am confused. Why is ID returning undefined rather than 1?

I expected to be returned:

[
  {1, #PID<0.973.0>, :worker, [MyApp.Statistic.Server]}
]

It doesn't matter number, atom or module. Identifier always returns undefined.

Is it Elixir's bug?

Upvotes: 1

Views: 331

Answers (1)

Dmitry Krakosevich
Dmitry Krakosevich

Reputation: 305

I found solution

http://erlang.org/doc/man/supervisor.html#which_children-1

id - it is always :undefined for dynamic supervisors

Upvotes: 2

Related Questions