1
votes

I have a DynamicSupervisor with basic settings, I am starting a child GenServer as

Supervisor.start_child(Recording.WorkerStarter, %{
  id: String.to_atom(detailed_camera.name),
  camera: detailed_camera,
  sleep: detailed_camera.sleep
})

and Terminating it as

name
|> Process.whereis()
|> case do
  nil -> :not_found
  pid -> DynamicSupervisor.terminate_child(__MODULE__, pid)
end

I want to catch its termination, as when I terminate this child, I want to Log something.

I have tested terminate/2 in Recording.WorkerStarter but it won't get called.

I have tried handle_info({:DOWN, ref, :process, _, _}, state) it wont work.

How I can catch Recording.WorkerStarter termination?

UPDATE:

this is my worker

defmodule Recording.WorkerStarter do
  use GenServer

  require Logger

  def start_link(opts) do
    IO.inspect("Started Recording.WorkerStarter")
    {id, opts} = Map.pop!(opts, :id)
    GenServer.start_link(__MODULE__, opts, name: id)
  end

  def init(state) do
    schedule_fetch_call(state.sleep)
    {:ok, state}
  end
1

1 Answers

1
votes

I assume you return :stop from init/1 callback. The documentation on GenServer.terminate/2 states:

terminate/2 is called if a callback (except init/1) [...]

That said, you might go through GenServer.handle_continue/2 and return :stop from there, or trap exists from the calling process with Process.flag(:trap_exit, true).