kgrvamsi

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

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().

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement