pub struct Supervisor { /* private fields */ }Expand description
A minimal supervisor that coordinates shutdown and tracks child threads.
Blessed pattern:
- Create one
Supervisorat process start. - Spawn long-running threads with
Supervisor::spawn_thread(orSupervisor::spawn_restartable_thread), and exit the thread when shutdown is requested. - On shutdown (signal / admin request), call
Supervisor::shutdown_and_join.
§Error semantics
- A child thread that returns
ErrcausesSupervisor::join_allto returnErr. - A child thread that panics also causes
Supervisor::join_allto returnErr. When multiple children fail, errors are aggregated into a single message.
Implementations§
Source§impl Supervisor
impl Supervisor
Sourcepub fn new(cfg: SupervisorConfig) -> Self
pub fn new(cfg: SupervisorConfig) -> Self
Create a new supervisor with the provided configuration.
Contract: configuration is validated when spawning children.
Sourcepub fn config(&self) -> SupervisorConfig
pub fn config(&self) -> SupervisorConfig
Get the supervisor configuration.
Sourcepub fn token(&self) -> ShutdownToken
pub fn token(&self) -> ShutdownToken
Convenience accessor for the supervisor shutdown token.
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Validate the current configuration.
Errors: returns Error if the configuration is internally inconsistent.
Sourcepub fn spawn_thread<F>(&self, name: impl Into<String>, f: F) -> Result<()>
pub fn spawn_thread<F>(&self, name: impl Into<String>, f: F) -> Result<()>
Spawn a supervised thread.
Contract: the closure should return when it observes shutdown.
Errors:
- Returns an error if configuration validation fails.
- Returns an error if the OS thread cannot be spawned.
If the closure returns Err, Supervisor::join_all will return Err.
Sourcepub fn spawn_restartable_thread<F>(
&self,
name: impl Into<String>,
f: F,
) -> Result<()>
pub fn spawn_restartable_thread<F>( &self, name: impl Into<String>, f: F, ) -> Result<()>
Spawn a supervised thread that will be restarted with backoff on error.
Contract:
- The child exits successfully if shutdown is requested.
- If the closure returns
Errand shutdown is not requested, the supervisor sleeps for a backoff duration and retries.
Errors:
- Returns an error if configuration validation fails.
- Returns an error if the OS thread cannot be spawned.
- Returns an error if the child exceeds
max_restarts.
Sourcepub fn shutdown_and_join(&self) -> Result<()>
pub fn shutdown_and_join(&self) -> Result<()>
Convenience: trigger shutdown and then join all children.
Contract: this is equivalent to calling Shutdown::trigger and then
Supervisor::join_all.
Trait Implementations§
Source§impl Clone for Supervisor
impl Clone for Supervisor
Source§fn clone(&self) -> Supervisor
fn clone(&self) -> Supervisor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more