Let It Crash: Creating an Example Supervisor in Elixir Using OTP

Tyler Pachal
Nov 10, 2017 · 5 min read

Part 0: New Mix Project

mix new my_supervisor

Part 1: What do supervisors do?

lib/Parser.ex: A flakey worker that tries to parse all incoming messages as integers
Testing the Parser in iex by sending it valid and invalid messages and watching it crash the parent iex process
Testing the Parser in iex by sending it valid and invalid messages with the parent iex process trapping exits
iex console screenshot

Part 2: Building Our Own Supervisor

Let it crash!

lib/my_supervisor.ex: First iteration with the ability to start child functions, and list the child processes
Starting up a MySupervisor process and verifying that is has linked to child processes
Sending messages to the Parser process that is supervised by the first iteration of MySupervisor.
{:EXIT, #PID<0.124.0>, reason}
lib/my_supervisor.ex: A new callback for the supervisor so that it can handle exiting child processes
Final test of the supervisor: verifying that it restarts dying children processes

Conclusion

Tyler Pachal

Written by

Software engineer at PagerDuty, working with Scala and Elixir.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade