Hello Everyone
I’m working on a project which needs to use dynamic supervisor to support creating genserver based workers .
The code today can fork/spawn a worker and get attached to the parent dynamic supervisor but when i want to terminate i still see the process as part of the dynamic supervisor process.
Here is the code on the same
Dynamic supervisor code
defmodule Testing.WorkerSupervisor do
@moduledoc """
Worker Supervisor to support dynamic workers
"""
use DynamicSupervisor
require Logger
def start_link(_) do
Logger.debug("Starting the Worker Supervisor")
DynamicSupervisor.start_link(__MODULE__, [], name: __MODULE__)
end
@impl true
def init(_) do
DynamicSupervisor.init(strategy: :one_for_one)
end
def start_task_worker(conn_id) do
spec = {Testing.ConnectionWorker, %{conn_id: conn_id}}
DynamicSupervisor.start_child(__MODULE__, spec)
end
def terminate_task_worker(pid) do
Logger.info("Terminating the Worker")
case DynamicSupervisor.terminate_child(__MODULE__, pid) do
:ok ->
Logger.info("Worker Terminated Successfully")
{:error, _} ->
Logger.info("PID not found")
end
end
end
The Processcheck worker who is part of the main supervisor i have this code
defmodule Testing.ConnectionWorker do
@moduledoc false
use GenServer, restart: :transient
state = %{
conn_info: %{
running: [%{
pid: "pid_id",
conn_name: "new worker"
}]
}
}
Enum.each(diff, fn conn ->
Enum.each(conn_info.running, fn run ->
if conn == run.conn_name do
IO.inspect(run.pid)
Testing.WorkerSupervisor.terminate_task_worker(run.pid)
end
end)
end)
When i see the process association with :observer.start i still see the worker attached to the Dynamic supervisor.
Am i doing the implementation wrong here?