pub struct Supervisor<E: Event, T: Topic<E> = DefaultTopic> { /* private fields */ }Expand description
Coordinates actors and the broker, and owns the top-level runtime.
§Actor Registration
// Subscribe to specific topics
supervisor.add_actor("processor", |ctx| Processor::new(ctx), &[MyTopic::Data])?;
// Subscribe to all topics (e.g., monitoring)
supervisor.add_actor("monitor", |ctx| Monitor::new(ctx), Subscribe::all())?;
// Subscribe to no topics (pure event producer)
supervisor.add_actor("producer", |ctx| Producer::new(ctx), Subscribe::none())?;§Runtime Control
start()spawns the broker loop and returns immediately (non-blocking).send(event)emits events into the broker.run()combinesstart()andjoin(). Consumes the supervisor.join()awaits all actor tasks to finish. Consumes the supervisor.stop()graceful shutdown. Consumes the supervisor.
The terminal methods (run, join, stop) take ownership of the supervisor,
preventing use-after-shutdown at compile time.
Implementations§
Source§impl<E: Event, T: Topic<E>> Supervisor<E, T>
impl<E: Event, T: Topic<E>> Supervisor<E, T>
Sourcepub fn new(config: SupervisorConfig) -> Self
pub fn new(config: SupervisorConfig) -> Self
Create a new supervisor with the given runtime configuration.
Sourcepub fn add_actor<A, F, S>(
&mut self,
name: &str,
factory: F,
topics: S,
) -> Result<ActorId>
pub fn add_actor<A, F, S>( &mut self, name: &str, factory: F, topics: S, ) -> Result<ActorId>
Register a new actor with a factory that receives a Context<E>.
This is the primary way to register actors with the supervisor.
§Arguments
name- Actor identifier used for metadata and routingfactory- Closure that receives a Context and returns the actortopics- Slice of topics the actor subscribes to
§Errors
Returns Error::DuplicateActorName if an actor with the same name
is already registered. Returns Error::BrokerAlreadyStarted if
called after start().
§Example
supervisor.add_actor(
"processor",
|ctx| DataProcessor::new(ctx),
&[MyTopic::Data, MyTopic::Control]
)?;Sourcepub fn build_actor<'a, A, F>(
&'a mut self,
name: &str,
factory: F,
) -> ActorBuilder<'a, E, T, A>
pub fn build_actor<'a, A, F>( &'a mut self, name: &str, factory: F, ) -> ActorBuilder<'a, E, T, A>
Start building an actor registration with custom configuration.
Returns an ActorBuilder that lets you set topics, channel capacity,
or a full ActorConfig before calling build().
Use this instead of add_actor when you need
per-actor settings that differ from the global defaults.
§Example
sup.build_actor("consumer", |ctx| Consumer::new(ctx))
.topics(&[Topic::Data, Topic::Command])
.channel_capacity(512)
.build()?;Sourcepub async fn send<IE: Into<IntoEnvelope<E>>>(&self, into_envelope: IE) -> Result
pub async fn send<IE: Into<IntoEnvelope<E>>>(&self, into_envelope: IE) -> Result
Emit an event into the broker from the supervisor.
§Errors
Returns Error::MailboxClosed if the broker channel is closed.
Sourcepub async fn start(&mut self) -> Result
pub async fn start(&mut self) -> Result
Start the broker loop in a background task. This returns immediately.
§Errors
Currently infallible, but returns Result for forward compatibility.
Sourcepub async fn join(self) -> Result
pub async fn join(self) -> Result
Waits until at least one of the actor tasks completes then triggers a shutdown if not already requested.
§Errors
Returns Error::Internal if an actor task panics.
Propagates any error returned by stop().
Sourcepub async fn stop(self) -> Result
pub async fn stop(self) -> Result
Request a graceful shutdown, then await all actor tasks.
§Shutdown Process
- Waits for the broker to receive all pending events (up to 10 ms)
- Sends
StopBrokercommand and waits for the broker to drain actor queues - Sends
StopRuntimecommand and waits for all actor tasks to complete
§Errors
Returns Error::Internal if an actor task panics during shutdown.
Sourcepub fn config(&self) -> &SupervisorConfig
pub fn config(&self) -> &SupervisorConfig
Returns the supervisor’s configuration.
Sourcepub fn monitors(&mut self) -> &mut MonitorRegistry<E, T>
Available on crate feature monitoring only.
pub fn monitors(&mut self) -> &mut MonitorRegistry<E, T>
monitoring only.Returns the monitor registry for adding, removing, and controlling monitors.
Source§impl<E: Event, T: Topic<E> + Label> Supervisor<E, T>
impl<E: Event, T: Topic<E> + Label> Supervisor<E, T>
Sourcepub fn to_mermaid(&self) -> String
pub fn to_mermaid(&self) -> String
Generate a Mermaid flowchart showing actor subscriptions.
Topics are shown as circles, actors as boxes. Arrows indicate that an actor subscribes to (receives events from) a topic.
Actors with Subscribe::all() are connected to all known topics.
Actors with Subscribe::none() appear isolated (no incoming arrows).
§Example output
flowchart LR
SensorData((SensorData)) --> processor
SensorData --> logger
Alert((Alert)) --> loggerTopic names are obtained via Topic::name().
Source§impl<E: Event, T: Topic<E> + Label> Supervisor<E, T>
impl<E: Event, T: Topic<E> + Label> Supervisor<E, T>
Sourcepub fn to_json(&self) -> Result<String>
Available on crate feature serde only.
pub fn to_json(&self) -> Result<String>
serde only.Export actor subscription topology as JSON.
This method provides a machine-readable representation of which actors
are subscribed to which topics. It mirrors the information shown by
to_mermaid, but returns structured JSON suitable
for inspection, tooling, or testing.
The output is a flat list where each entry contains:
actor_id- the actor namesubscriptions- topic labels the actor receives events from
§Semantics
- Actors registered with
Subscribe::all()are expanded to include all known topics discovered from explicit subscriptions. - Actors registered with
Subscribe::none()produce an empty list. - Topic names are obtained via
Label::label().
This export reflects declared routing configuration only. It does not represent runtime message flow, event producers, or supervision hierarchy.
§Errors
Returns any serialization error produced by serde_json.
§Example
let json = supervisor.to_json()?;
println!("{json}");