kgrvamsi

kgrvamsi

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?

Showing Posts 1 to 6

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

kgrvamsi

kgrvamsi OP

Hi @kokolegorille thanks for your response and just to my knowledge want to know what is the difference between the Dynamic Supervisor terminating its child vs child terminating it self over a call?

and also checking on your suggestion that terminating the child from the genserver it self so was checking around some example for it and saw this

defmodule Server do
  use GenServer

  def start do
    GenServer.start(__MODULE__, []) 
  end 

  def stop(pid) do
    GenServer.call(pid, :stop)
  end 

  def handle_call(:stop, _from, status) do
    {:stop, :normal, status}
  end 

  def terminate(reason, _status) do
    IO.puts "Asked to stop because #{inspect reason}"
    :ok 
  end 
end

Is this what you are recommending and any directions on how to fetch the child process pid in the supervisor and send a call to the Genserver to stop?

kokolegorille

kokolegorille

I always use DynamicSupervisor with Registry to help finding those workers by name.

kgrvamsi

kgrvamsi OP

@kokolegorille i’m using the ets tables and saving the genserver pid and the name associated to it and when i’m trying to kill it i’m sending a call to the genserver stop function to shutdown the process in normal mode.

send(PID, :stop)

in the Genserver

 def handle_call(:stop, _from, state) do
    {:stop, :normal, state}
  end

Please advice if i’m doing it in the right way.

kokolegorille

kokolegorille

The code is ok for stoping the GenServer, althought You might also use a cast instead of call.

The registry is ets based, no real need to duplicate it’s functionality by hand.

kgrvamsi

kgrvamsi OP

@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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews