Second argument passed to Registry.keys/2 is a pid of a process which put an entry into registry (i.e. the one which called Registry.register/3 which is done behind the scenes when using :via option). I think that in your case it is a pid of MySupervisor because this is the process which called start_link/1 of your workers.
Try it with supervisor’s pid and let us know if that worked!
Thank for the answer @arkgil!
In the end I figured out how Registry works. It links a key/value to a process which used to register it. So in my case each id is linked to a process (on starting procedure using :via) and when this process dies, the key automatically deletes from Registry (because linked to it).
Answer is – it’s impossible to iterate through these ids in Registry, but it’s possible to use Supervisor.which_children and get all children pids instead
I also use Registry to store an additional data for processes (using processes itself to register data), and it automatically cleans when a process killed.
I have to say, I was initially a bit perplexed by Registry.keys/2. I think I misunderstood its functionality based on some incorrect, preconceived notions.
I discovered this thread when trying to figure out my use case, so I figured I’d add a post in case it’s useful to someone in future.
I just wanted to get a list of all registered keys in a registry and assumed (without reading the docs to be fair) that Registry.keys/2 would do that, in much the same way that Registry.count/1 returns a count of all items in a registry.
I read through the docs a bit and realised that Registry.select/2does this; i.e:
# get a list of keys
Registry.select(MyRegistry, [{{:"$1", :_, :_}, [], [:"$1"]}])
# get a list of two-element key/pid tuples
Registry.select(MyRegistry, [{{:"$1", :"$2", :_}, [], [{{:"$1", :"$2"}}]}])