l3nz

l3nz

Finding ids of children in DynamicSupervisor

I am trying to run a list of tasks under a Dynamic Supervisor. Imagine that it is a URL downloader. I need to know if they are still running, and maybe talk to them to change their configuration in-flight.

In order to talk to them, each task is registered as {Download, "www.example.com"} in a global registry. Now, the application runs a dynamic number of sub-applications, so that there is

  • {DynamicSupervisor “customer-a”}
  • {DynamicSupervisor “customer-b”}

So I basically want to know which Download modules are running under {DynamicSupervisor "customer-a"} - so I call it and get a list of children like:

[{:undefined, #PID<0.14119.0>, :worker, [Downloader]},
{:undefined, #PID<0.14123.0>, :worker, [Downloader]}]

The issue is that only the module is reported back, so I cannot know which one it is - I can get the PID, get all the pids out of Registry, and keep the ones that match both, so I can look up the same information. But this seems quite a mouthful.

Is there a way to change the name of the module in the output, or tag it somehow? The first field is supposed to be the :id and it would be perfect, but in DynamicSupervisor, as per the docs (why?) it is always :Undefined.

Most Liked

lud

lud

I would register tasks with {client_id, url} in the registry.
So you can use Registry.match to filter keys for a given client_id.

Note that you may not need multiple supervisors, you can start all downloaders under the same. Except if your “sub applications” are OTP applications.

felix-starman

felix-starman

Take a look at the DynamicSupervisor’s extra_arguments. I use it all the time in combo with a via_tuple function and child_spec function on the children to namespace the child processes within the registry. You could use the customer id for example.

The other option is to manually register the children into another registry during the init with the customer id passed by the extra_argument.

Let me know if you need an example. I’m just on a tablet, or I’d have posted one

felix-starman

felix-starman

As to why is it :undefined, and why can’t it be set, it’s because DynamicSupervisor doesn’t enforce the id.

In a normal Supervisor, the id is used to ensure that if you’re supervising the same type of process multiple times (imagine like a connection pooler), that it can manage them separately, start them in the correct order, restart them in the correct order, or do other necessary steps for other supervision strategies. That id has to be known before the child process is started, because the a typical Supervisor has to be able to make those decisions for the strategy as part of the children startup.

In a DynamicSupervisor, there’s no concept of other supervision strategies, because all the children are essentially independent of one another, so you don’t need to worry about distinguishing them prior to their startup, since you start them up later, after the DynamicSupervisor is already up (or at least in a handle_continue, after the init)

Some questions that’ll help me better understand your use-case

  • What’s the actual reason you need know which children are running under a given DynamicSupervisor?
  • Do you need to be able to “know” value/domain of the Downloader before calling it? or is just having the list of children enough
  • It seems like you’re wanting to know if multiple Download processes are for the same domain, but different customers. Do you only want to run one but have it be “linked” to multiple customers? Do you want to have multiple processes but just need to be able to quickly check for the existence of other Download processes for the same domain?

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement