Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference previously started processes in an Elixir supervisor

I am starting a Supervisor that monitors two children. The second child needs a reference to the first. It seams like this should be possible because by using the one_for_rest strategy I can make sure that if the first dies the second is restarted.

children = [
  supervisor(SupervisorA, [arg1]),
  supervisor(SupervisorB, [arg2, ref_to_supervisor_a_process]),
]

supervise(children, strategy: :one_for_rest)

Ideally without having to globally name either process.

like image 946
Peter Saxton Avatar asked May 16 '26 03:05

Peter Saxton


1 Answers

SupervisorA can supply the name: option to Supervisor.start_link/3.

SupervisorB can then use Process.whereis/1 to resolve the name to a pid or just send messages to the named process.

https://hexdocs.pm/elixir/Supervisor.html#start_link/3 https://hexdocs.pm/elixir/Process.html#whereis/1

like image 103
Mike Buhot Avatar answered May 18 '26 21:05

Mike Buhot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!