Editing
Supervisor (Elixir)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Supervisor (Elixir) == [[File:Supervisor logo.png|right|thumb|200px|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 === 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 === 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 === 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 === 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 === 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 === * [[Application (Elixir)|Application (Elixir)]] * [[GenServer (Elixir)|GenServer (Elixir)]] == References == [https://hexdocs.pm/elixir/Supervisor.html Elixir Supervisor Documentation] [[Category: Elixir Programming Language]] [[Category: Concurrency Patterns]]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information