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.
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.
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:
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.
So all of these workers actually have a single parent
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
How often do you query this info? How often does this subset of state change? Do you want to see changes to this state in live view automatically or only on user-initiated update?
This will be queried when the liveview mounts so whenever a user visits the page (hopefully many users in the future but currently just me), then receiving updates preferably as live as possible but it’s not strictly necessary, current implementation is doing 1 update per second and it’s good enough.
On the worker side, the genserver state is being updated 1 to 10 times a second, possibly higher in the future.