Implement a Supervisor

Learn to make our supervisor from scratch and add it to our project.

So far, we have used two types of supervisors—Task.Supervisor and DynamicSupervisor. They’re great because they allow us to start processes on demand. However, they are both made with a specific goal in mind, and sometimes we need greater configurability over how the supervisor works.

Implement a Supervisor from scratch

We will often opt for the Supervisor behaviour to create a supervisor from scratch. In this section, we create a supervisor using the Supervisor behaviour and put it in front of each Job process.

We already learned that supervisors could restart child processes and saw how the :permanent and :transient restart values work. The only setting we haven’t used yet is :temporary. This value tells the supervisor to ignore the process and not worry about restarting it even when the child process exits with an error.

Our goal is to make JobRunner start an intermediary supervisor for each job, which starts the actual Job process. This new supervisor will restart the Job process if needed but will have a restart value of :temporary. Therefore, it won’t cause further damage if it fails. This figure shows how the supervision tree is going to look after we make all the changes:

Get hands-on with 1200+ tech skills courses.