Supervisor (Elixir)

From Elixir Wiki
Jump to navigation Jump to search

Supervisor (Elixir)[edit]

File:Supervisor logo.png
Supervisor logo

The Supervisor module in Elixir is a behavior implemented by the `GenServer` module that provides a powerful mechanism for managing and monitoring child processes. It is used widely in Elixir applications to build fault-tolerant systems by supervising and restarting failed child processes.

Overview[edit]

A supervisor is a process that monitors and restarts its child processes when necessary. It plays a crucial role in building fault-tolerant systems by isolating and restarting failed components. In Elixir, supervisors are created using the `Supervisor` module, which implements the supervisor behavior.

The primary responsibilities of a supervisor include:

  • Starting child processes
  • Restarting failed child processes
  • Stopping child processes gracefully
  • Managing the life cycle of child processes

Supervisors are typically structured in a hierarchical manner, forming a supervision tree. The top-level supervisor is called the **root supervisor**, and it is responsible for managing other supervisors and child processes below it in the tree.

Starting Child Processes[edit]

Supervisors use *child specifications* to define and configure child processes. A child specification is a simple data structure that describes how a child process should be started and supervised. It includes information such as the module to start, the arguments to pass, and restart strategies.

To start a child process, a supervisor calls the `start_child/2` function of the Supervisor module, passing the child specification as an argument. The supervisor then starts the child process and keeps track of its state.

Restarting Failed Child Processes[edit]

When a child process crashes or terminates unexpectedly, the supervisor is notified and takes appropriate actions based on the configured restart strategy. Elixir provides several restart strategies to control the behavior of supervisors during failures, including:

  • `:one_for_one`: Restart only the failed child process.
  • `:one_for_all`: Restart all child processes in the tree.
  • `:rest_for_one`: Restart the failed child process and all processes started after it.
  • `:simple_one_for_one`: A variant of `:one_for_one` for dynamically started child processes.

The restart strategy is specified when defining the supervisor using the `Supervisor.init/1` function.

Stopping Child Processes Gracefully[edit]

Supervisors can gracefully stop child processes by calling their `terminate/2` function. Child processes can implement this function to perform cleanup tasks or notify other processes before exiting.

To stop a child process, the supervisor calls the `terminate_child/2` function, passing the child specification or the pid of the child as an argument.

Managing the Life Cycle[edit]

The life cycle of a supervisor is closely tied to the life cycle of the application it belongs to. Supervisors are typically started when an application starts and stopped when the application shuts down.

To start a supervisor, the `Supervisor.start_link/2` function is used, which sets up the supervision tree, initializes child processes, and returns a supervisor process identifier.

Similarly, the `Supervisor.stop/1` function is used to stop a supervisor and its child processes gracefully. When a supervisor is stopped, all child processes are terminated, and any resources they hold are released.

See Also[edit]

References[edit]

Elixir Supervisor Documentation