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

Last Post!

kgrvamsi

kgrvamsi

@kokolegorille thanks for the reply … your guidance is really helping me and making me love elixir more .

Here is the something i made a change as per your suggestions

@impl true
def handle_cast(:stop, state) do
    {:stop, :normal, state}
  end 

Infact i’m not directly using the ets table but using this library which is kinda wrapper on top of ETS ({:ets, “~> 0.7.3”}) but i will take your suggestions on using registry.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement