Start child after it was terminated

Hello People, I have Supervisor that starts children dynamically with an ID {Wms.Queue.Worker.Dummy, 2} I want that child to be terminated and I am doing it with

  def handle_info(:timeout, state) do
    {:stop, :normal, state}
  end

I’m using the option restart: :transient so when it terminates the supervisor does not restart it, But I want to start_child manually later.
When I try to start it again it tells me

** (exit) exited in: GenServer.call(Wms.Queue.Worker.Dummy, {:terminate_child, {Wms.Queue.Worker.Dummy, 2}}, :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

And it is because the children are still registered in the supervisor with a undefined PID

Supervisor.which_children(Wms.Queue.Worker.Dummy.Pool)
[
  {{Wms.Queue.Worker.Dummy, 2}, :undefined, :worker, [Wms.Queue.Worker.Dummy]},
  {{Wms.Queue.Worker.Dummy, 1}, :undefined, :worker, [Wms.Queue.Worker.Dummy]}
]

Could someone explain to me why the terminated child is not deleted from there, and which is the best way?

Is it a DynamicSupervisor?

If not, you should convert it to such and then manually start the children initially. This should give you the freedom to then manually restart them whenever the time is right.

1 Like

Can you show how you start the process initially? Is the process linked to the dynamic supervisor?

It took me some time to convert it, but at the end it was the solution, Do you know If I Can put names to the children of the dynamic supervisor?

Check out the Supervisor top-level docs, they explain to you the possible ways to provide a child_spec for the started sub-tasks. Yes, you can have whatever ID/name you like for them.

1 Like