pub struct Supervisor { /* private fields */ }Expand description
Supervises a set of workers.
§Workers
All workers are defined through implementation of the Supervisable trait, which provides the logic for both
creating the underlying worker future that’s spawned, as well as other metadata, such as the worker’s name, how the
worker should be shutdown, and so on.
Supervisors also (indirectly) implement the Supervisable trait, allowing them to be supervised by other
supervisors in order to construct supervision trees.
§Instrumentation
Supervisors automatically create their own allocation group
([TrackingAllocator][resource_accounting::TrackingAllocator]), which is used to track both the memory usage of the
supervisor itself and its children. Additionally, individual worker processes are wrapped in a dedicated
tracing::Span to allow tracing the causal relationship between arbitrary code and the worker executing it.
§Restart Strategies
As the main purpose of a supervisor, restart behavior is fully configurable. A number of restart strategies are available, which generally relate to the purpose of the supervisor: whether the workers being managed are independent or interdependent.
All restart strategies are configured through RestartStrategy, which has more information on the available
strategies and configuration settings.
Implementations§
Source§impl Supervisor
impl Supervisor
Sourcepub fn new<S: AsRef<str>>(supervisor_id: S) -> Result<Self, SupervisorError>
pub fn new<S: AsRef<str>>(supervisor_id: S) -> Result<Self, SupervisorError>
Creates an empty Supervisor with the default restart strategy.
Sourcepub fn with_restart_strategy(self, strategy: RestartStrategy) -> Self
pub fn with_restart_strategy(self, strategy: RestartStrategy) -> Self
Sets the restart strategy for the supervisor.
Sourcepub fn with_auto_shutdown(self, auto_shutdown: AutoShutdown) -> Self
pub fn with_auto_shutdown(self, auto_shutdown: AutoShutdown) -> Self
Sets the supervisor’s automatic-shutdown policy.
Controls whether the termination of significant children (see ChildSpecification::with_significant) drives the
supervisor to shut down. Defaults to AutoShutdown::Never.
Sourcepub fn with_shutdown_mode(self, mode: ShutdownMode) -> Self
pub fn with_shutdown_mode(self, mode: ShutdownMode) -> Self
Sets the supervisor’s shutdown mode. See ShutdownMode. Defaults to ShutdownMode::Ordered.
Sourcepub fn handle(&self) -> SupervisorHandle
pub fn handle(&self) -> SupervisorHandle
Returns a handle for spawning dynamic children on this supervisor while it runs.
The handle can be created before the supervisor starts and cloned freely. Spawns only succeed while the
supervisor is actually running; if it hasn’t started yet, is between restarts, or has shut down, they return
SpawnError::SupervisorGone.
Sourcepub fn with_dedicated_runtime(self, config: RuntimeConfiguration) -> Self
pub fn with_dedicated_runtime(self, config: RuntimeConfiguration) -> Self
Configures this supervisor to run in a dedicated runtime.
When this supervisor is added as a child to another supervisor, it will spawn its own OS threads and Tokio runtime instead of running on the parent’s ambient runtime.
This provides runtime isolation, which can be useful for:
- CPU-bound work that shouldn’t block the parent’s runtime
- Isolating failures in one part of the system
- Using different runtime configurations (for example, single-threaded vs multi-threaded)
Sourcepub fn add_worker<S, T>(&mut self, child: T)
pub fn add_worker<S, T>(&mut self, child: T)
Adds a worker (or nested supervisor) to the supervisor.
A worker can be anything that implements the Supervisable trait. A Supervisor can also be added as a
worker and managed in a nested fashion, known as a supervision tree.
See ChildSpecification for more details on how workers are represented internally and what options are
available to configure.
Sourcepub async fn run(&mut self) -> Result<(), SupervisorError>
pub async fn run(&mut self) -> Result<(), SupervisorError>
Runs the supervisor forever.
§Errors
If the supervisor exceeds its restart limits, or fails to initialize a child process, an error is returned.
Sourcepub async fn run_with_shutdown<F: Future + Send + 'static>(
&mut self,
shutdown: F,
) -> Result<(), SupervisorError>
pub async fn run_with_shutdown<F: Future + Send + 'static>( &mut self, shutdown: F, ) -> Result<(), SupervisorError>
Runs the supervisor until shutdown is triggered.
When shutdown resolves, the supervisor will shutdown all child processes according to their shutdown strategy,
and then return.
§Errors
If the supervisor exceeds its restart limits, or fails to initialize a child process, an error is returned.
Trait Implementations§
Source§impl From<Supervisor> for ChildSpecification<SupervisorSpec>
impl From<Supervisor> for ChildSpecification<SupervisorSpec>
Source§fn from(supervisor: Supervisor) -> Self
fn from(supervisor: Supervisor) -> Self
Auto Trait Implementations§
impl !RefUnwindSafe for Supervisor
impl !UnwindSafe for Supervisor
impl Freeze for Supervisor
impl Send for Supervisor
impl Sync for Supervisor
impl Unpin for Supervisor
impl UnsafeUnpin for Supervisor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Track for T
impl<T> Track for T
§fn track_resources(self, token: ResourceGroupToken) -> Tracked<Self>
fn track_resources(self, token: ResourceGroupToken) -> Tracked<Self>
Tracked wrapper. Read more§fn in_current_resource_group(self) -> Tracked<Self>
fn in_current_resource_group(self) -> Tracked<Self>
Tracked wrapper. Read more