Search code examples
elixirerlang-supervisorgen-server

Creating a Supervisor having multiple children elixir


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: 512, start: {Gossip_node, :start_link, [[0, true]]}, type: :worker}, %{id: 49677, start: {Gossip_node, :start_link, [[0, true]]}, type: :worker},

followed by the list of all the children processes. The processes have different ID's

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.


Solution

  • :simple_one_for_one strategy is deprecated in favour of DynamicSupervisor

    Anyway, let's have a quick look in the docs when it wasn't deprecated:

    :simple_one_for_one - similar to :one_for_one but suits better when dynamically attaching children. This strategy requires the supervisor specification to contain only one child.

    Thus, you either need to change the strategy of the supervisor to :one_for_one (or some other one):

    Supervisor.init(children, strategy: :simple_one_for_one)
    

    or create a supervisor with one child (or with no children if using DynamicSupervisor) and attach every child dynamically.