Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
About the Author
About the Reviewers

Creating a supervisor

One of the main advantages of Elixir is fault tolerance, and one of the underlying philosophies is the famous let it crash philosophy. This means that by principle, no defensive programming is performed. You write the code that expresses your intent and handles the case you are expecting, and then if something goes wrong, you just let the process crash.

There are mechanisms in Elixir that allow the monitoring of processes and even give you the ability to relaunch a process (or a group of processes) if something goes wrong.

Probably in case of programming errors, this doesn't make sense, but what if the error was due to something that's external to your program? What if the program is logically sound, and everything is working as it's supposed to but, say, a resource, such as a network, fails? Your processes might crash because of that. What if there was a mechanism that would allow you to try again? Fortunately, there is: supervisors!

This is another OTP-defined behavior...