How to init 2 DynamicSupervisors under one Supervisor?

the problem is that when in Supervisor you try this:

children = [
  {
    DynamicSupervisor,
    name: SomeName,
    strategy: :one_for_one
  },
  {
    DynamicSupervisor,
    name: AnotherName,
    strategy: :one_for_one
  }
]

IEX show this message

Blockquote
Application papelito exited: Papelito.Application.start(:normal, ) returned an error: shutdown: failed to start child: Papelito.Supervisor.Root
** (EXIT) bad child specification, more than one child specification has the id: DynamicSupervisor.
If using maps as child specifications, make sure the :id keys are unique.
If using a module or {module, arg} as child, use Supervisor.child_spec/2 to change the :id, for example:

children = [
  Supervisor.child_spec({MyWorker, arg}, id: :my_worker_1),
  Supervisor.child_spec({MyWorker, arg}, id: :my_worker_2)
]

** (Mix) Could not start application papelito: Papelito.Application.start(:normal, ) returned an error: shutdown: failed to start child: Papelito.Supervisor.Root
** (EXIT) bad child specification, more than one child specification has the id: DynamicSupervisor.
If using maps as child specifications, make sure the :id keys are unique.
If using a module or {module, arg} as child, use Supervisor.child_spec/2 to change the :id, for example:

children = [
  Supervisor.child_spec({MyWorker, arg}, id: :my_worker_1),
  Supervisor.child_spec({MyWorker, arg}, id: :my_worker_2)
]

The error message hints to the proper solution, which would be:

children = [
  Supervisor.child_spec({
    DynamicSupervisor,
    name: SomeName,
    strategy: :one_for_one
  }, id: :dynamic_1),
  Supervisor.child_spec({
    DynamicSupervisor,
    name: AnotherName,
    strategy: :one_for_one
  }, id: :dynamic_2)
]

Have fun!

6 Likes

Thank you José, It works.