Puentes de supervisión y jerarquías de procesos mixtas
Integre procesos que no pertenecen a OTP y subsistemas externos en su árbol de supervisión mediante supervisor_bridge, manteniendo intacta la tolerancia a fallos.
Puentes de supervisión y jerarquías de procesos mixtas es una lección gratuita de Erlang OTP: Distributed & Fault-Tolerant Systems Programming en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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/1starts the real subsystem and returns its pidterminate/2cleanly 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_bridgewraps non-OTP subsystemsinit/1starts and links the subsystem;terminate/2stops 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
Preguntas frecuentes
¿La lección «Puentes de supervisión y jerarquías de procesos mixtas» es gratis?
Sí — el texto completo de «Puentes de supervisión y jerarquías de procesos mixtas» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, actualiza a CoddyKit PRO. El curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming incluye 4 lecciones en total.
¿Qué aprenderé en «Puentes de supervisión y jerarquías de procesos mixtas»?
Integre procesos que no pertenecen a OTP y subsistemas externos en su árbol de supervisión mediante supervisor_bridge, manteniendo intacta la tolerancia a fallos. Practicas Erlang OTP: Distributed & Fault-Tolerant Systems Programming con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
No se requiere experiencia previa. Erlang OTP: Distributed & Fault-Tolerant Systems Programming en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Puentes de supervisión y jerarquías de procesos mixtas»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Sí. Cada lección de Erlang OTP: Distributed & Fault-Tolerant Systems Programming incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Árboles de supervisión complejos
- Gestión dinámica de procesos
- Estrategias avanzadas de reinicio
- Puentes de supervisión y jerarquías de procesos mixtas