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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews