Introduce DynamicSupervisor
Learn to use the dynamic supervisors to start GenServer processes.
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:
Press + to interact
#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 widgetdef start(_type, _args) dochildren = [{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 ...