Registry and "listeners" option

I have an application that spawns an arbitrary number of processes (little processes) under a supervisor, and a single other process (registered with name BigProcess) that would like to know when new little processes start or if they end.

I looked at Registry and noticed the listeners option, and since my little processes all start using :via tuples to a local registry, I thought maybe that would work for me. So I added

{Registry, keys: :unique, name: LittleProcessRegistry, listeners: [BigProcess]

to my supervisor tree, but I get an error

:erlang.send(BigProcess, {:register, LittleProcessRegistry, LittleProcess.Name, #PID<0.291.0>, nil})

This is (I assume) because the Registry tries to immediately start sending notifications to the big process but it hasn’t finished starting up yet. But I can’t find a way to set the listener after it has started.

Am I misunderstanding how this is meant to be used? Or maybe there’s a better way to do this?

Your BigProcess should be started earlier in the supervision tree than the registry, then you can be sure it’s already started before the registry will receive registrations.

3 Likes

That’s the correct answer. I never really thought about the start order before now. Thanks!

https://ferd.ca/the-hitchhiker-s-guide-to-the-unexpected.html is a good blogpost to read on that

5 Likes