ollien

ollien

Discovering a task supervisor local to my supervision tree

I was writing a GenServer and quickly discovered the need to generate tasks within this GenServer. This GenServer can not have a globally unique name (as I require many instances of it. I decided to keep the current module structure, changing the GenServer to a Supervisor and spawning my original GenServer as a child of this supervisor (with the Supervisor having public functions that call the child GenServer). Ideally, my supervision tree would look something like this

     MyServerSupervisor 
              /\
             /  \
            /    \
   MyGenServer   MyTaskSupervisor

Also, ideally, I would spawn my tasks within MyTaskSupervisor. However, the obvious question arises: how does MyGenServer discover this task supervisor? In other projects, I’ve given this task supervisor a globally unique name, but I can’t do that here.

Looking into the problem, I’ve seen many suggestions to use a Registry, but those require globally unique names. In order to have a registry for my task supervisors, I’d have to spawn the Registry in my root supervisor to ensure all of my processes can find it. It would be a lot nicer if I could somehow register the task supervisor in the context MyServerSupervisor. Is there a way to achieve what I want, or is the closest analog the solution I just described?

Thanks!

First Post!

al2o3cr

al2o3cr

Question about naming:

How do you tell these apart? That’s where a Registry would usually come in handy.

For instance, imagine that each instance can be identified by an Erlang term that we’ll call id. Could be a database ID, could be a UUID, could be an ARN, whatever.

Start a single top-level Registry instance, we’ll name it MyServerRegistry for concreteness.

Then instances of MyServerSupervisor would name themselves like:

{:via, Registry, {MyServerRegistry, id}}

While instances of MyGenServer and MyTaskSupervisor could be named like:

{:via, Registry, {MyServerRegistry, id, :server}}
# vs
{:via, Registry, {MyServerRegistry, id, :task_supervisor}}

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement