kirqe

kirqe

Start children of dynamic supervisor after it's started

Having a list of strings where every string represents an argument for a child, how can I start them all after DynamicSupervisor.init? Or should I use plain Supervisor? Or am I doing it wrong at all? Thanks

If I add lines |> Enum.each(&add/1) after DynamicSupervisor.init, it doesn’t seem to work and as I understand it’s a bad practice to do this inside init.

application.ex

    children = [
      {Pbot.Stash, []},
      {Pbot.SymbolSupervisor, ["one", "two", "three"]}
    ]

symbol_supervisor.ex

defmodule Pbot.SymbolSupervisor do
  use DynamicSupervisor

  def start_link(lines) do
    DynamicSupervisor.start_link(__MODULE__, lines)
  end

  def init(lines) do
    DynamicSupervisor.init(strategy: :one_for_one)
    # lines |> Enum.each(&add/1)
  end

  def add(line) do
    {:ok, _pid} = DynamicSupervisor.start_child(__MODULE__,  {Pbot.M, line})
  end
end

error

04:18:17.411 [info]  Application pbot exited: Pbot.Application.start(:normal, []) returned an error: shutdown: failed to start child: Pbot.SymbolSupervisor
    ** (EXIT) exited in: GenServer.call(Pbot.SymbolSupervisor, {:start_child, {{Pbot.M, :start_link, [[%Pbot.Pair{...}, %Pbot.Pair{...}]]}, :transient, 5000, :worker, [Pbot.M]}}, :infinity)
        ** (EXIT) process attempted to call itself

Marked As Solved

blatyo

blatyo

Conduit Core Team

You probably just want a normal supervisor if you know all the processes that are going to exist at startup.

Also Liked

rvirding

rvirding

Creator of Erlang

Two problems with your code:

  • You need to give a :name option to the DymanicSupervisor.start_link call as you assume it has been set to the module name.
  • You are making a synchronous call to yourself as the DynamicSupervisor.start_child call is made from inside the supervisor process. If this had been allowed the supervisor process would hang. This what the 2nd and 3rd error lines are about. The 1st error line comes from this supervisor’s supervisor exiting because its children crashed at startup.

And you have the issue which @blatyo mentioned: do you really want/need a normal supervisor here as you know all child processes? The only difference here would be the termination order of the children.

Where Next?

Popular in Questions Top

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
Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
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
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement