aaronrenner

aaronrenner

Phoenix Core Team

Best practices for connecting Supervisor's child processes to each other

I am trying to figure out if there’s an established best practice for connecting child processes within the same supervisor without using name registration.

I have the following processes

  • PortCache
  • StateServer
  • WebServer - requires a PortCache and StateServer for startup

I am using a rest_for_one supervision strategy and trying to create a supervision tree like this.

I’m curious what the best way is to give WebServer a way to send messages to PortCache and WebServer without using name registration.

Here is what I currently have:

defmodule MyApp.Supervisor do

  def start_link(_init_opt) do
    # Start the supervisor with no children
    with {:ok, sup} <- Supervisor.start_link([], strategy: :rest_for_one) do
      {:ok, port_cache} = Supervisor.start_child(sup, PortCache)
      {:ok, state_server} = Supervisor.start_child(sup, StateServer)

      {:ok, _web_server} = 
        Supervisor.start_child(sup, {
          WebServer, 
          port_cache: port_cache, 
          state_server: state_server
         })

       {:ok, sup}
    end
  end
end

I know generally we pass a list of children to a supervisor, but then we don’t have a way to reference the other process’s pids. Are there drawbacks to this approach or a better way of doing this?

Most Liked

kokolegorille

kokolegorille

I would do something similar, but not with WebServer, with an additional GenServer used to start others processes, and keeping pid in his state.

I guess I am a little bit influenced by how poolboy does this.

rcavanaugh

rcavanaugh

Maybe overkill but what about Registry? Start a registry named MyApp.Supervisor.Registry in your MyApp.Supervisor before anything else and then each supervised process can register itself in init and dispatch calls via the registry.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

aalberti333
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
rms.mrcs
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement