Virviil

Virviil

It’s very easy to supervise :simple_one_for_one children with Registry. The Registry is unique, and via tuple works very good.
But the question is: How to dispatch to all these children?
It seems to me, that is possible to dispatch only to duplicate registry!
Why it’s not possible to list all registered in Registry pid’s?

The workaround is - using Supervisor.which_children get all pids and dispatch messages to them. But it looks a bit rusty.

Showing Posts 1 to 4

Ankhers

Ankhers

Have you taken a look at the Using as a dispatcher section of the documentation? If that still doesn’t answer your question, can you give a bit more information about what you are trying to do?

sasajuric

sasajuric

Author of Elixir In Action

You could use Registry.match/3 for that:

Registry.start_link(:unique, MyRegistry)
Registry.register(MyRegistry, :foo, nil)
spawn(fn -> Registry.register(MyRegistry, :bar, nil); :timer.sleep(:infinity) end)
Registry.match(MyRegistry, :_, :_)
# [{#PID<0.80.0>, nil}, {#PID<0.86.0>, nil}]

However, I feel that it’s better to have another (:duplicate) registry for dispatching. The unique registry should be used when you want to find a particular process which plays some role in your system. The duplicate registry should be used when you want to dispatch to a group of processes. Those are two different needs, so I believe that using different registries describes the idea more explicitly. For what it’s worth, this is how I’d do it myself. If a process needs to be uniquely found, but also belongs to some process group, I’d register it with two registries.

Virviil

Virviil OP

Two registeries is great idea as for me from the point of good architect. I thought about it.

But the problem is with consistency:

Registration in duplicate registry is doing manualy, as I understand. For example, in init function:

def init(_) do
  Registry.register(MyRegistry, "worker_class_name", "my_worker_name")
end

But if the supervisor restarts the simple_one_for_one worker after failure, PID is updated in only uniq register.

So, as I understand, init should be rewrited in such manner:

def init(_) do
  # check here, that registry already have entry with such name, and if not
  Registry.register(MyRegistry, "worker_class_name", "my_worker_name")
  # and if yes - firstly remove that entry, but hard to understand how, and then
  Registry.register(MyRegistry, "worker_class_name", "my_worker_name")
end

So, may be there are some recipes of doing this already? Or may be Registry need some additional functionality? Or even, may be my approach is totally wrong.

sasajuric

sasajuric

Author of Elixir In Action

It’s going to work fine for both registries. If the process terminates, it’s deregistered from all the registries. When the new process is started, it will be properly registered with both registries. You don’t need to check whether the process is already registered, nor remove it from the registry manually.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews