eidge

eidge

DynamicSupervisor starting child with more than one argument

Hey there guys,

I’m trying to use a DynamicSupervisor to start multiple children, however I need to register them with a specific name. However from the documentation there seems to be no way of doing this, unless I pass a map to the start_child/2 function:

DynamicSupervisor.start_child(pid, {Pipeline, job_module})

What would be the best way of starting and registering this child with a name?

Most Liked

josevalim

josevalim

Creator of Elixir

The second element of the tuple can be a keyword list, where you would pass multiple options:

{Pipeline, job_module: job_module, name: ...}

In case the Pipeline module does not support the new child specs, then you need to build a child specification by hand:

%{
  id: Pipeline,
  start: {Pipeline, :start_link, [job_module, [name: ...]]
}
peerreynders

peerreynders

I interpreted his advice to go with the fully featured map-based version of the spec:

%{
  id: Pipeline,
  start: {Pipeline, :start_link, [job_module, [name: ...]]
}

rather than the tuple short form, not necessarily modifying start_link.

GenServer (which it is) start_link/2

FYI: actually it’s start_link/3

start_link(module, args, options \\ [])

Ultimately it’s this line

  start: {Pipeline, :start_link, [job_module, [name: ...]]

that determines how the child process is started. Pipeline is simply the module that owns the startin function, :start_link identifies the function to “start” the process (it could be set :fred - and then you better provide a fred function) and the list that follows are the arguments to be used when calling start_link (or fred).

So there really is no default signature - the tuple spec assumes a name of start_link but that is a mere convention but there is no fixed arity.

Ultimately the function is run with something like Kernel.apply/3 - so the number of elements in the args list ties into the function’s arity - keeping in mind that the last element can be keyword list to mimic variadic arguments (I personally find it confusing when the enclosing brackets are left out “for convenience”).

Just to crudely demonstrate:

defmodule Pipeline do
  use Supervisor

  def init(args) do
    {:ok, args}
  end

  defp queue_name(job_module),
    do: String.to_atom("#{job_module}-queue")

  def spec_map(job_module),
    do: %{
      id: Pipeline,
      start: {
        Pipeline,
        :fake_start_link,
        [job_module, [name: queue_name(job_module)]]
      }
    }

  def fake_start_link(job_module, opts) do
    IO.puts("job_module: #{inspect job_module}");
    IO.puts("opts: #{inspect opts}")
    // GenServer.start_link(job_module, [], opts)
  end

end

job_module = Queue;
spec = Supervisor.child_spec(Pipeline, Pipeline.spec_map(job_module))
IO.inspect spec
{m,f,a} = spec.start
Kernel.apply(m,f,a)
$ elixir demo.exs
%{
  id: Pipeline,
  start: {Pipeline, :fake_start_link, [Queue, [name: :"Elixir.Queue-queue"]]},
  type: :supervisor
}
job_module: Queue
opts: [name: :"Elixir.Queue-queue"]
josevalim

josevalim

Creator of Elixir

I don’t think that will happen because you always need to implement the GenServer callbacks and that’s when you will wrap its start_link/3 by your own start_link/1. I would never expect somebody to start a {GenServer, …} directly in a supervision tree.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

We're in Beta

About us Mission Statement