prateek1192

prateek1192

Creating a supervisor with multiple children

I am trying to take an input from the user and then creating the number of genservers as input and Supervise them. My code is something like

defmodule GossSim do
  use Supervisor

def main(args) do
  # Since it is a mix generated project I had to put the main
  Supervisor.start_link(__MODULE__, args)
end

def start_link(args) do
  Supervisor.start_link(__MODULE__, args)
end

#The two methods down below create children dynamically
def get_child_list(child_list, 0), do: child_list

def get_child_list(child_list, num_of_nodes) do
  child = 
    %{  
    id: :rand.uniform(num_of_nodes*100000),
    start: {Gossip_node, :start_link, [[0,true]]}
  }  
  if num_of_nodes > 0 do
   
    get_child_list( [child] ++ child_list, num_of_nodes-1)
  end
end

def init(args) do
  children = get_child_list([], 10)
  # The outut of this inspect is pasted below
  IO.inspect children, label: "The children list is"
  // some function that does something parse_args_and_start(args)
  // num_of_nodes is 10
  children = get_child_list([], num_of_nodes)
  Supervisor.init(children, strategy: :simple_one_for_one)

end

I get the following error
bad child specification, invalid children: [%{id: 83303, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 186070, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 85483, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 40851, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 85216, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 390428, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 466922, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 187412, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 103750, start: {Gossip_node, :start_link, [[0, true]]}}, %{id: 176391, start: {Gossip_node, :start_link, [[0, true]]}}]

The Supervisor doc says that a supervisor is fine if it has a start and an id. Since children is a list I am adding multiple maps of children in it. Am i missing something. Gossip_node is a module in the same folder.

Marked As Solved

rvirding

rvirding

Creator of Erlang

In one sense these are not really dynamic children. Yes, we are starting input specified number of children but from the supervisors POV it is starting the children all at start-up time so they are not dynamic. They could just as well be specified as a list of children to start.

Also there are no problems with dynamically starting children in a “normal” Supervisor. There is a Supervisor.start_child/2 which does this. This is the same as for DynamicSupervisor which also has a DynamicSupervisor.start_child/2. Both these functions need a child-spec. The main difference is how the children are terminated: in a Supervisor they are terminated in reverse order to which they were started; while in a DynamicSupervisor they are all terminated at the same time.

:simple_one_for_one is/was slightly different in that when you started the supervisor you gave one child spec which is then used as a template for all the children started/managed by it. It is/was a special case for running lots of the same thing.

Both Supervisor and DynamicSupervisor have a terminate_chlid/2 for killing children.

Also Liked

prateek1192

prateek1192

Thanks for looking into it @kokolegorille
I started looking into that but then they say that

The Supervisor module was designed to handle mostly static children that are started in the given order when the supervisor starts. A DynamicSupervisor starts with no children. Instead, children are started on demand via start_child/2

I don’t need to start children on demand. When my supervisor starts up it knows what processes it needs to supervise. Anyways with that Dynamic Supervisor too I might have run into the same error when trying to supervise multiple children as I would have tried to add them into the list.

rvirding

rvirding

Creator of Erlang

In this case :one_for_one would be the best choice, although that will probably not fix this error.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
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 t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

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
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement