defmodule My.Supervisor do
use Supervisor
def start_link(opts) do
Supervisor.start_link(__MODULE__, :ok, opts)
end
def init(:ok) do
children = [
{DynamicSupervisor, name: GamesSupervisor, strategy: :one_for_one}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
DySup Module :
defmodule DySup do
def start_child(game_id) do
DynamicSupervisor.start_child(GamesSupervisor, %{
id: game_id,
start: {__MODULE__, :start_link, [:ok]},
type: :worker,
restart: :transient,
shutdown: 500
})
end
def start_link(args) do
{:ok, args}
end
end
inside terminal (iex -S mix)
when i do : DySup.start_child(1)
why i am getting error : {:error, {:ok, :ok}}
defmodule DySup do
def start_child() do
DynamicSupervisor.start_child(GamesSupervisor,Gen)
end
def show_childs do
DynamicSupervisor.which_children(GamesSupervisor)
end
def end_child(pid) do
Process.exit(pid, :kill)
end
end
Gen Module :
defmodule Gen do
use GenServer
def child_spec(opts) do
%{
id: Gen,
start: {__MODULE__, :start_link, [opts]},
shutdown: 5_000,
restart: :transient,
type: :worker
}
end
def start_link(state \\ []) do
GenServer.start_link(__MODULE__, state)
end
def init(state), do: {:ok, state}
def handle_call(:hi,_from, state) do
IO.puts("hi received")
{:reply,state,state}
end
end
But, when i call : DySup.end_child(pid("0.240.0"))
a new process start , i tried overriding child spec of Gen module, i even set restart: :temporary, but still a new process start.
defmodule DySup do
def start_child() do
DynamicSupervisor.start_child(GamesSupervisor, Gen)
end
def show_childs do
DynamicSupervisor.which_children(GamesSupervisor)
end
def end_child(pid) do
Process.exit(pid, :kill)
end
def stop(pid), do: GenServer.cast(pid, :stop)
end
defmodule Gen do
use GenServer
def child_spec(opts) do
%{
id: Gen,
start: {__MODULE__, :start_link, [opts]},
shutdown: 5_000,
restart: :transient,
type: :worker
}
end
def start_link(state \\ []) do
GenServer.start_link(__MODULE__, state)
end
def init(state), do: {:ok, state}
def handle_call(:hi, _from, state) do
IO.puts("hi received")
{:reply, state, state}
end
def handle_cast(:stop, state), do: {:stop, :normal, state}
end
But when i add :
I am getting :
iex(1)> DySup.start_worker(1)
** (exit) exited in: GenServer.call(DySup, {:start_child, {{Worker, :start_link, [1, %{}]}, :transient, 5000, :worker, [Worker]}}, :infinity)
** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
(elixir) lib/gen_server.ex:999: GenServer.call/3
Also i want to know, is it is good thing to have one child specification with same id to create multiple supervisor’s childrens because as per definition :
id - Required key. Used by the supervisor to identify the child specification.
As all childs will run same code in parallel differentiated using their pids.