0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · Lekcja

Mosty supervisorów i mieszane hierarchie procesów

Integruj procesy spoza OTP i zewnętrzne podsystemy z drzewem nadzoru za pomocą supervisor_bridge, zachowując odporność na błędy.

Mosty supervisorów i mieszane hierarchie procesów to bezpłatna lekcja Erlang OTP: Distributed & Fault-Tolerant Systems Programming na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Erlang OTP: Distributed & Fault-Tolerant Systems Programming, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Why a Supervisor Bridge?

Not every process you depend on is a clean OTP gen_server. Sometimes you must supervise a plain Erlang process, a port, or a third-party subsystem. The supervisor_bridge behaviour lets such a process appear as a normal child in a supervision tree.

The Problem It Solves

A standard supervisor only knows how to start, stop and restart OTP-compliant children. A raw process started with spawn_link does not respond to OTP shutdown protocols. The bridge translates between the two worlds.

Behaviour Callbacks

A supervisor_bridge module implements two callbacks:

  • init/1 starts the real subsystem and returns its pid
  • terminate/2 cleanly shuts the subsystem down

Skeleton Module

The bridge is started like a supervisor but behaves like a worker to its parent.

-module(legacy_bridge).
-behaviour(supervisor_bridge).
-export([start_link/0, init/1, terminate/2]).

start_link() ->
    supervisor_bridge:start_link({local, ?MODULE}, ?MODULE, []).

Implementing init/1

init/1 must start and link the external process, then return {ok, Pid, State}. If start fails, return {error, Reason}.

init([]) ->
    case legacy_server:start() of
        {ok, Pid} -> {ok, Pid, Pid};
        Error -> Error
    end.

Implementing terminate/2

When the bridge is asked to shut down, it calls terminate/2 so you can stop the subsystem gracefully.

terminate(_Reason, Pid) ->
    legacy_server:stop(Pid),
    ok.

Placing the Bridge in a Tree

To its parent supervisor, the bridge is just another child spec with type => worker.

ChildSpec = #{id => legacy,
              start => {legacy_bridge, start_link, []},
              restart => permanent,
              type => worker}.

Failure Propagation

Because the bridge links to the external process, a crash there exits the bridge, which the parent supervisor sees as a normal worker crash and restarts according to its strategy.

Mixed Hierarchies

A real system mixes gen_server workers, sub-supervisors, and bridges. The bridge lets legacy or foreign code participate without weakening the OTP guarantees of the rest of the tree.

When NOT to Use a Bridge

If you can wrap the subsystem in a proper gen_server, prefer that — it gives you full OTP introspection. Reserve the bridge for code you cannot rewrite or that has its own process lifecycle.

Debugging Bridged Subsystems

Because the inner process is not OTP, tools like sys:get_state/1 will not reach it. Add logging inside the subsystem and rely on the bridge state to track the inner pid.

Quick Check

Test your understanding of supervisor bridges.

Recap

You learned to integrate foreign processes into supervision trees:

  • supervisor_bridge wraps non-OTP subsystems
  • init/1 starts and links the subsystem; terminate/2 stops it
  • It appears as a worker child to its parent supervisor
  • Crashes propagate so normal restart strategies apply
  • Prefer a real gen_server when you control the code

Często zadawane pytania

Czy lekcja „Mosty supervisorów i mieszane hierarchie procesów” jest bezpłatna?

Tak — pełny tekst „Mosty supervisorów i mieszane hierarchie procesów” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Erlang OTP: Distributed & Fault-Tolerant Systems Programming, przejdź na CoddyKit PRO. Kurs Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera 4 lekcji w sumie.

Co nauczysz się w „Mosty supervisorów i mieszane hierarchie procesów”?

Integruj procesy spoza OTP i zewnętrzne podsystemy z drzewem nadzoru za pomocą supervisor_bridge, zachowując odporność na błędy. Ćwiczysz Erlang OTP: Distributed & Fault-Tolerant Systems Programming z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

Nie wymagamy żadnego doświadczenia. Erlang OTP: Distributed & Fault-Tolerant Systems Programming w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Mosty supervisorów i mieszane hierarchie procesów”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

Tak. Każda lekcja Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Złożone drzewa nadzoru
  2. Dynamiczne zarządzanie procesami
  3. Zaawansowane strategie ponownego uruchamiania
  4. Mosty supervisorów i mieszane hierarchie procesów
← Powrót do Erlang OTP: Distributed & Fault-Tolerant Systems Programming