Search⌘ K

Introduce DynamicSupervisor

Explore how to introduce and use DynamicSupervisor in Elixir to manage GenServer processes on demand. Understand the importance of the start_link/1 function, how to add DynamicSupervisor to your supervision tree, and troubleshoot common errors that arise during dynamic process management.

The DynamicSupervisor supervisor

DynamicSupervisor is another ready-to-use supervisor available to us. It can start any GenServer process on demand. Let’s add it to the supervision tree:

C++
#file path -> jobber/lib/jobber/application.ex
#add this code at the indicated place mentioned in comments of jobber/lib/jobber/application.ex
#in the playground widget
def start(_type, _args) do
children = [
{DynamicSupervisor, strategy: :one_for_one, name: Jobber.JobRunner},
]
opts = [strategy: :one_for_one, name: Jobber.Supervisor]
Supervisor.start_link(children, opts)
end

The strategy setting is required, and the only currently accepted strategy is :one_for_one. We talk about supervisor strategies later in this chapter, so we don’t need to worry about this for now.

The module-based DynamicSupervisor supervisor

We can also define a DynamicSupervisor module as we see in the later lessons. Instead of use ...