ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Elixir Supervisor Restart Strategy

Elixir

Goal -- WPM

Ready
Exercise Algorithm Area
1defmodule MyApp.Supervisor do
2use Supervisor
3
4@doc """
5Starts the supervisor with its children.
6"""
7def start_link(init_arg) do
8Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
9end
10
11@impl true
12def init(_arg) do
13children = [
14# Define your child processes here
15worker(MyApp.Worker, [], id: MyApp.Worker)
16]
17
18# Configure the supervisor with a restart strategy
19opts = [
20strategy: :one_for_one,
21max_restarts: 5,
22max_seconds: 60
23]
24
25{:ok, {self(), opts, children}}
26end
27end
28
29defmodule MyApp.Worker do
30use GenServer
31
32@doc """
33Starts the worker process.
34"""
35def start_link(opts) do
36GenServer.start_link(__MODULE__, :ok, opts)
37end
38
39@impl true
40def init(:ok) do
41IO.puts("Worker started")
42{:ok, %{counter: 0}}
43end
44
45@impl true
46def handle_call(:increment, _from, state) do
47new_state = %{state | counter: state.counter + 1}
48IO.puts("Counter incremented to: #{new_state.counter}")
49{:reply, :ok, new_state}
50end
51
52@impl true
53def handle_info(:crash, state) do
54# Simulate a crash
55raise "Intentional crash!"
56{:noreply, state}
57end
58
59# Handle unexpected messages gracefully
60def handle_info(message, state) do
61IO.puts("Received unexpected message: #{inspect(message)}")
62{:noreply, state}
63end
64end
Algorithm description viewbox

Elixir Supervisor Restart Strategy

Algorithm description:

This Elixir code defines a supervisor that manages a single worker process. The supervisor is configured with a `:one_for_one` restart strategy, meaning if a child process crashes, only that specific child will be restarted. This is crucial for building fault-tolerant applications where individual components can fail without bringing down the entire system. A common use case is managing a pool of worker processes that perform independent tasks.

Algorithm explanation:

The `MyApp.Supervisor` module uses Elixir's `Supervisor` behavior to manage child processes. The `init/1` callback defines the children and the supervisor's options. The `:one_for_one` strategy ensures that if `MyApp.Worker` crashes (e.g., due to an exception in `handle_info(:crash, state)`), the supervisor will attempt to restart only `MyApp.Worker`. The `max_restarts` and `max_seconds` options prevent runaway restarts by limiting the number of restarts within a given time frame. The `MyApp.Worker` is a `GenServer` that maintains a simple counter state. Its `handle_call/3` function handles synchronous requests to increment the counter, and `handle_info/2` is used to simulate a crash. The time complexity for starting the supervisor and its children is O(N), where N is the number of children. Space complexity is O(1) for the supervisor itself, and O(S) for each worker, where S is the size of its state.

Pseudocode:

Define a Supervisor module.
In the init callback:
  Define a list of child specifications.
  Set the supervisor strategy to :one_for_one.
  Configure max_restarts and max_seconds.
  Return {:ok, {self(), opts, children}}.
Define a GenServer worker module.
Implement start_link, init, handle_call, and handle_info callbacks.
In handle_info, simulate a crash by raising an exception.