kgrvamsi
Dynamic supervisor terminate child issues
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?
First Post!
kokolegorille
Hello welcome,
Try to change this…
use GenServer, restart: :transient
# to
use GenServer, restart: :temporary
… to test if it restarts or not (it should never restart). If not, it means on terminate_child, the worker does not stop with normal status, and so it is restarted because of transient.
BTW I usually stop GenServer from within, with a specific message, or under some conditions, but not from the supervisor itself, and not with terminate_child().
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








