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
Conduit Core Team
You probably just want a normal supervisor if you know all the processes that are going to exist at startup.
1
Also Liked
rvirding
Creator of Erlang
Two problems with your code:
- You need to give a
:nameoption to theDymanicSupervisor.start_linkcall as you assume it has been set to the module name. - You are making a synchronous call to yourself as the
DynamicSupervisor.start_childcall 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.
3
Popular in Questions
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
can someone please explain to me how Enum.reduce works with maps
New
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
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
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
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
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
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
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
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
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
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
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...
New
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
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







