Since OTP 25.1 we have a process groups monitoring so I finally made an implementation of distributed Registry
leveraging :pg
and an [almost] drop-in distributed replacement for local DynamicSupervisor
.
❯ iex --sname n1 -S mix
iex(n1@am-victus)1> DistributedSupervisor.start_link name: DS, listeners: DistributedSupervisor.Test.Listener, cache_children?: true, monitor_nodes: true
{:ok, #PID<0.254.0>}
iex(n1@am-victus)2> Node.connect :"n2@am-victus"
true
iex(n1@am-victus)3> DistributedSupervisor.start_child DS, {DistributedSupervisor.Test.GenServer, name: Foo1}
{:ok, #PID<24436.263.0>, Foo1}
iex(n1@am-victus)4> DistributedSupervisor.start_child DS, {DistributedSupervisor.Test.GenServer, name: Foo2}
{:ok, #PID<24436.264.0>, Foo2}
iex(n1@am-victus)5> DistributedSupervisor.start_child DS, {DistributedSupervisor.Test.GenServer, name: Foo3}
{:ok, #PID<0.265.0>, Foo3}
iex(n1@am-victus)6> DistributedSupervisor.children DS
%{
Foo1 => #PID<24436.263.0>,
Foo2 => #PID<24436.264.0>,
Foo3 => #PID<0.265.0>,
}
iex(n1@am-victus)7> DistributedSupervisor.whereis DS, Foo1
#PID<24436.263.0>
iex(n1@am-victus)8> DistributedSupervisor.via_name DS, Foo1
{:via, DistributedSupervisor.Registry, {DS, Foo1}}
iex(n1@am-victus)9> GenServer.whereis v()
#PID<24436.263.0>
It supports listener(s) to be attached to each DistributedSupervisor
“instance,” identified by a name, to be informed about processes going up/down and nodes going up/down.
Enjoy.