quiq

quiq

Prevent Dynamic Supervisor from restarting a process on normal exit

I have a dynamic supervisor that starts a gen server that should terminate itself after a specified time. However, the supervisor seems to restart the process, even when restart option is set to transient.

Here’s the relevant code (redacted so there may be errors, but you’ll get the idea):

Dynamic supervisor:

def init(:ok) do
    DynamicSupervisor.init(strategy: :one_for_one, restart: :transient)
  end

def start_genserver() do
    spec = %{
      start: {Module.Name, :start_link, []},
      restart: :transient
    }

    DynamicSupervisor.start_child(__MODULE__, spec)
  end

in the gen server:

@impl true
  def init(state) do
    Process.send_after(self(), :terminate, 3_000)
    {:ok, state}
  end

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

However the Gen Server process keeps coming back with a different PID. What am I missing here?

Most Liked

al2o3cr

al2o3cr

I think you may have redacted something important, because I can’t reproduce the behavior you’re seeing. This script can be run with just elixir foo.exs:

defmodule FooSupervisor do
  use DynamicSupervisor

  def start_link(init_arg) do
    DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  def init(:ok) do
    DynamicSupervisor.init(strategy: :one_for_one, restart: :transient)
  end

  def start_genserver() do
    spec = %{
      id: "nope",
      start: {FooWorker, :start_link, []},
      restart: :transient
    }

    IO.puts("starting child")
    DynamicSupervisor.start_child(__MODULE__, spec)
  end
end

defmodule FooWorker do
  use GenServer

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

  @impl true
  def init(state) do
    IO.puts("child init")
    Process.send_after(self(), :terminate, 3000)
    {:ok, state}
  end

  @impl true
  def handle_info(:terminate, state) do
    IO.puts("child stop")
    {:stop, :normal, state}
  end
end

FooSupervisor.start_link(:ok)

FooSupervisor.start_genserver()

Process.sleep(:infinity)

When I run this, I get:

starting child
child init
child stop

and then nothing else.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement