prateek1192
Correct way to create Genservers using Dynamic Supervisor
I am trying to create a dynamic supervisor to start child processes. There are three modules I am using to do this, one is the main module, the second one is the Dynamic Supervisor and the third is the genserver. I do not get any errors but I am not able to see any child processes running.
Here is my code. The first one is the module having the main method
defmodule Simulator do
def main(args) do
{:ok, super_pid} = PersonManager.start_link(args)
start_persons(num_of_persons)
IO.inspect PersonManager.count_children, label: "The Persons started in main method are"
end
def start_persons(num_of_persons) when num_of_persons >=1 do
PersonManager.add_node(private_key, public_key, 0)
start_persons(num_of_persons-1)
end
# num_of_persons == 0 case handled
end
The following is a dynamic Supervisor
defmodule PersonManager do
use DynamicSupervisor
def start_link(args) do
DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__)
end
def add_node(private_key, public_key, num_of_coins) do
# The code flow does come here, used inspect to check
child_spec = {Person, {private_key, public_key, num_of_coins}}
DynamicSupervisor.start_child(__MODULE__, child_spec)
#The code flow does come here, used inpect to check.
end
end
The following is the GenServers the worker is supposed to create
defmodule Person do
use GenServer
def init({private_key, public_key, address, num_of_coins}) do
IO.puts "Starting a person"
{:ok, %{private_key: private_key, public_key: public_key, num_of_coins: num_of_coins}}
end
def start_link({private_key, public_key, address, num_of_coins}) do
IO.puts "Starting a person"
GenServer.start_link(
__MODULE__,
{private_key, public_key, address, num_of_coins}
name: {:global, "node:#{public_key}"},
)
end
end
I expect the genserver to be running but I get The Persons started in main method are: %{active: 0, specs: 0, supervisors: 0, workers: 0}
I dont know why the supervisor is not able to start the genserver.
Any help would be good.
TIA
First Post!
benwilson512
Author of Craft GraphQL APIs in Elixir with Absinthe
Can you show this function count_children ?
Popular in Questions
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
Other popular topics
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
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
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
I would like to know what is the best IDE for elixir development?
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
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New








