andzdroid

andzdroid

I have a worker process that lives for some time and contains some state. Worker processes can be spawned from multiple processes, so there is no single source that is the “parent” of these workers.

I want to show a dashboard of all the worker processes that are running and some information from their state. I want to show this all in a liveview, preferably on page load or soon after, so preferably all in one go, not as a page that slowly populates from a stream of data.

What is the best way for my liveview to gather all this information living in different processes?

My current solution is for the workers to regularly report their state to a gen server that keeps track of the workers, and the liveview calls this tracker process on mount. The tracker also listens for the death of workers so it can remove their info. Is there a better way to do this? This tracker feels like a single point of failure, and is also duplicating all the state that is already stored in the workers.

Showing Posts 1 to 10

LostKobrakai

LostKobrakai

For the “which workers are there” question you can use Registry. I’d however also consider spawning those workers under a shared supervisor.

andzdroid

andzdroid OP

They are already registered with a registry, and started under a single dynamic supervisor. I can get a list of PIDs, but I want to get the state in the workers, not just the PIDs.

LostKobrakai

LostKobrakai

You can put the information you need as the key in the Registry.

andzdroid

andzdroid OP

Thanks! I’ll give it a try

andzdroid

andzdroid OP

I have some follow-up questions about the registry:

Since I want to look up all registered processes, I assume I need to use Process.lookup/2. This requires all the workers to register under the same key in a duplicate registry:

iex > {:ok, _} = Registry.register(RegistryName, "shared_key", state)

Lookup all the entries:

iex > Registry.lookup(RegistryName, "shared_key")
[{pid1, state1}, {pid2, state2}, ...]

So far so good. But how do I update the value when the worker state is changed?

There is Registry.update_value/4 but the docs say it can only be used on a unique registry, an error is raised if used on a duplicate registry.

But if I use a unique registry, how do I lookup all the values without knowing the keys?

Do I use a duplicate registry and when I want to update the state, I would unregister then register again immediately?

hlx

hlx

If you have a list of PID’s you can send them a message to get whatever you need from the state, right?

e.g.

GenServer.call(pid, :get_the_name_from_the_state)

or if for some reason you do not want to add the handle_call/3 for that to your GenServers you can use

:sys.get_state(pid)
LostKobrakai

LostKobrakai

You can use Registry.select for fetching everything registered in the registry. There’s no explicit API for that given this can be rather expensive depending on the number of processes registered.

hst337

hst337

What does that mean? Can’t you just start workers under a single supervisor?

andzdroid

andzdroid OP

Yes, they are all started under one dynamic supervisor

hst337

hst337

So all of these workers actually have a single parent :slight_smile:
What information are you gathering? You can use Registry as @LostKobrakai pointed out, or you can just use DynamicSupervisor.which_children/1 and then calling every process, querying information you need. The right decision depends on the information you’re gathering

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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews