Supervisor module

From Elixir Wiki
Jump to navigation Jump to search

Supervisor module[edit]

The **Supervisor module** is a key component in the **Elixir** programming language. It plays a crucial role in **Elixir's concurrency model** by supervising and managing **child processes**. In this article, we will explore the functionality and usage of the Supervisor module in detail.

Overview[edit]

The Supervisor module is part of the **Elixir standard library** and provides a **supervision behavior** for building **fault-tolerant systems**. It allows developers to create and manage a **supervisor process**, which in turn monitors and controls a group of **child processes**.

Functionality[edit]

The Supervisor module offers several important functions and features:

Start a Supervisor[edit]

To start a supervisor, the `start_link/2` function is used. It takes a **module** implementing the **Supervisor behavior** and a **list of child specifications**. Each child specification describes a child process, including its **module**, **arguments**, and **options**.

Child Process Restart Strategies[edit]

The Supervisor module provides different **restart strategies** that define how child processes should be restarted in case of termination. These strategies include:

  • `:one_for_one`: Restart only the terminated child process.
  • `:one_for_all`: Restart all child processes.
  • `:rest_for_one`: Restart the terminated child process and all child processes started after it.
  • `:simple_one_for_one`: Restart only one specified child process.

Child Process Supervision[edit]

The Supervisor module monitors child processes and automatically restarts them based on the specified restart strategy. It ensures that the system remains **fault-tolerant** and keeps running even if individual processes fail.

Dynamic Children Management[edit]

Supervisors allow for **dynamic child management**. Child processes can be added or removed during runtime using functions such as `start_child/2`, `delete_child/2`, and `terminate_child/2`.

Supervisor Strategies[edit]

Supervisors can use different strategies to supervise child processes. These strategies define how the supervisor behaves when a child process terminates unexpectedly. Some common strategies include:

  • `:one_for_one`: Restart only the terminated child process.
  • `:one_for_all`: Restart all child processes.
  • `:rest_for_one`: Restart the terminated child process and all child processes started after it.
  • `:simple_one_for_one`: Restart only one specified child process.

Usage Examples[edit]

Let's take a look at a few examples that demonstrate the usage of the Supervisor module:

Example 1: Simple Supervisor[edit]

```elixir defmodule MySupervisor do

 use Supervisor
 def start_link do
   Supervisor.start_link(__MODULE__, [])
 end
 @impl true
 def init(_) do
   children = [
     worker(MyChild1, []),
     worker(MyChild2, []),
     worker(MyChild3, [])
   ]
   supervise(children, strategy: :one_for_one)
 end

end ```

Example 2: Dynamic Child Management[edit]

```elixir defmodule DynamicSupervisor do

 use Supervisor
 def start_link do
   Supervisor.start_link(__MODULE__, [])
 end
 @impl true
 def init(_) do
   supervise_dynamic(strategy: :one_for_one, max_restarts: 3, max_seconds: 5) do
     [worker(MyDynamicChild, [])]
   end
 end

end ```

Conclusion[edit]

The Supervisor module is a powerful tool in the Elixir programming language for building fault-tolerant systems. By providing process supervision and automatic restarting of failed child processes, it ensures the stability and reliability of concurrent applications. Understanding the Supervisor module's functionality and correct usage is essential for developing robust Elixir applications.

For more information, see the dedicated articles on **[Supervision Principles](Supervision_Principles)** and **[Child Specification](Child_Specification)**.

Template:Programming languages Template:Elixir