0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน

ออกแบบแอปพลิเคชันที่ทนทานด้วยซูเปอร์ไวเซอร์สำหรับติดตามและเริ่มกระบวนการใหม่ เพื่อให้ระบบทนต่อความขัดข้อง

ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Building Resilient Systems

In concurrent applications, things can go wrong. Processes might crash due to unexpected errors or external issues.

Fault tolerance is the ability of a system to continue operating even when components fail. Elixir embraces a "let it crash" philosophy, meaning instead of trying to prevent every single crash, it focuses on gracefully recovering from them.

Guardians of Processes

This is where Supervisors come in! A supervisor is a special kind of process designed to monitor other processes (its "children").

  • If a child process crashes, the supervisor automatically restarts it.
  • This ensures your application remains stable and available.
  • Supervisors form the backbone of fault-tolerant Elixir applications.

Your First Supervisor

Let's define a simple supervisor module. It uses the Supervisor behavior, similar to how GenServer uses the GenServer behavior.

The init/1 callback is where you define the children it will supervise and its restart strategy.

defmodule MySupervisor do
  use Supervisor

  def start_link(init_arg) do
    Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  @impl true
  def init(_init_arg) do
    # No children yet, just the supervisor itself
    children = []
    Supervisor.init(children, strategy: :one_for_one)
  end
end

# --- Main execution part ---
# This simulates starting the supervisor and checking its status.
# In a real app, this would be part of an Application's start/2.
IO.puts("Attempting to start MySupervisor...")
{:ok, supervisor_pid} = MySupervisor.start_link([])
IO.puts("MySupervisor started with PID: #{inspect(supervisor_pid)}")

# Check if the supervisor process is alive
if Process.alive?(supervisor_pid) do
  IO.puts("Supervisor is alive!")
else
  IO.puts("Supervisor is NOT alive!")
end

Understanding Supervision Strategies

Supervisors have different strategies for handling child failures:

  • :one_for_one: Restarts only the child that crashed. This is the default and most common.
  • :one_for_all: If any child crashes, all other children are terminated and then all children are restarted.
  • :rest_for_one: If a child crashes, it and all children started *after* it are terminated and then restarted.

Choosing the right strategy depends on the dependencies between your processes.

Adding Supervised Children

To make a supervisor useful, it needs children! You define children using Supervisor.child_spec/2, which tells the supervisor how to start and manage a process.

Here, we define a simple MyWorker GenServer and add it as a child to our supervisor.

defmodule MyWorker do
  use GenServer

  def start_link(_opts) do
    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  @impl true
  def init(:ok) do
    IO.puts("MyWorker started!")
    {:ok, %{}}
  end

  @impl true
  def handle_call(:crash, _from, state) do
    IO.puts("MyWorker is crashing!")
    exit(:bad_state) # Simulate a crash
    {:reply, :ok, state} # This line won't be reached
  end
end

defmodule MySupervisorWithWorker do
  use Supervisor

  def start_link(init_arg) do
    Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  @impl true
  def init(_init_arg) do
    children = [
      # Define our worker as a child process
      Supervisor.child_spec(MyWorker, id: MyWorker)
    ]
    Supervisor.init(children, strategy: :one_for_one)
  end
end

# --- Main execution part ---
IO.puts("Starting supervisor with worker...")
{:ok, supervisor_pid} = MySupervisorWithWorker.start_link([])
IO.puts("Supervisor PID: #{inspect(supervisor_pid)}")

# Get the worker's PID
worker_pid = Process.whereis(MyWorker)
IO.puts("Initial MyWorker PID: #{inspect(worker_pid)}")

if Process.alive?(worker_pid) do
  IO.puts("Worker is alive and supervised.")
else
  IO.puts("Worker did not start correctly.")
end

Fault Tolerance in Action

Now, let's see the supervisor in action! We'll deliberately crash our MyWorker process, and the supervisor will automatically restart it.

Notice how the worker's Process ID (PID) changes, indicating a new process was spawned.

defmodule MyWorker do
  use GenServer

  def start_link(_opts) do
    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  @impl true
  def init(:ok) do
    IO.puts("MyWorker started!")
    {:ok, %{}}
  end

  @impl true
  def handle_call(:crash, _from, state) do
    IO.puts("MyWorker is crashing!")
    exit(:bad_state) # Simulate a crash
    {:reply, :ok, state} # This line won't be reached
  end

  def crash_it do
    GenServer.call(__MODULE__, :crash)
  end
end

defmodule MySupervisorWithWorker do
  use Supervisor

  def start_link(init_arg) do
    Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  @impl true
  def init(_init_arg) do
    children = [
      Supervisor.child_spec(MyWorker, id: MyWorker)
    ]
    Supervisor.init(children, strategy: :one_for_one)
  end
end

# --- Main execution part ---
IO.puts("Starting supervisor with worker...")
{:ok, _supervisor_pid} = MySupervisorWithWorker.start_link([])

worker_pid_before_crash = Process.whereis(MyWorker)
IO.puts("Worker PID before crash: #{inspect(worker_pid_before_crash)}")

# Crash the worker
IO.puts("Attempting to crash the worker...")
MyWorker.crash_it()
:timer.sleep(100) # Give supervisor a moment to restart

worker_pid_after_crash = Process.whereis(MyWorker)
IO.puts("Worker PID after crash: #{inspect(worker_pid_after_crash)}")

if worker_pid_before_crash != worker_pid_after_crash && Process.alive?(worker_pid_after_crash) do
  IO.puts("Worker was restarted by the supervisor! New PID detected.")
else
  IO.puts("Worker was NOT restarted, or PID remained the same (unexpected).")
end

Elixir Applications: The Top Level

While supervisors manage individual processes, an Elixir Application is the top-level unit of code and processes in an Elixir system.

It provides a structured way to:

  • Group related modules and processes.
  • Define how your system starts up and shuts down.
  • Manage configuration and dependencies.

The `Application` Behavior

Every Elixir application typically has a main application module that use Application.

The most important callback is start/2, which is invoked when your application starts. This is where you typically start your top-level supervisor, which then recursively starts all other processes in your system.

defmodule MyApp.Application do
  use Application

  # This is the entry point for your application.
  # It starts the top-level supervisor.
  @impl true
  def start(_type, _args) do
    children = [
      # In a real app, you'd start your main supervisor here.
      # For example: Supervisor.child_spec(MySupervisorWithWorker, id: MySupervisorWithWorker)
    ]

    # Start a supervisor that will supervise other processes/supervisors
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

# --- Main execution part ---
# This part simulates how an application would be started.
# In a real Mix project, 'mix run --no-halt' would call MyApp.Application.start/2
IO.puts("Simulating application start...")
{:ok, pid} = MyApp.Application.start(:normal, [])
IO.puts("Application top-level supervisor started with PID: #{inspect(pid)}")

if Process.alive?(pid) do
  IO.puts("Application supervisor is active.")
else
  IO.puts("Application supervisor failed to start.")
end

Hierarchical Supervision Trees

For complex applications, you often don't have just one supervisor. You create a supervision tree, where supervisors can supervise other supervisors.

  • This allows you to organize your application into logical units.
  • Different parts of your system can have different restart strategies.
  • If a major component fails, its supervisor can restart it without affecting unrelated parts of the system.

Supervisor Check

Time for a quick check on your understanding of supervisor strategies!

Lesson Summary

Well done! You've learned how Elixir builds resilient applications:

  • Supervisors monitor processes and restart them upon failure.
  • Different supervision strategies (:one_for_one, :one_for_all, :rest_for_one) dictate how failures are handled.
  • Elixir Applications provide the top-level structure, starting supervisors and forming supervision trees.

These concepts are fundamental to building robust, fault-tolerant systems in Elixir.

คำถามที่พบบ่อย

บทเรียน “ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน”

ออกแบบแอปพลิเคชันที่ทนทานด้วยซูเปอร์ไวเซอร์สำหรับติดตามและเริ่มกระบวนการใหม่ เพื่อให้ระบบทนต่อความขัดข้อง คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กระบวนการและการส่งข้อความใน Elixir
  2. การนำพฤติกรรม GenServer มาใช้
  3. ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน
  4. การทำงานพร้อมกันด้วย Task และ Agent
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development