pub struct Supervisor { /* private fields */ }Expand description
The central supervisor.
Manages the lifecycle of all TradingService implementations using
TaskTracker for structured concurrency and CancellationToken
for graceful shutdown propagation.
§Memory safety
Unlike JoinSet, TaskTracker does not accumulate task return
values. Completed task memory is reclaimed immediately, making this safe
for long-running processes that may restart services hundreds of times
over weeks of operation.
§Example
let supervisor = Supervisor::new(SupervisorConfig::default());
supervisor.spawn_service(Box::new(MyService));
supervisor.run_until_shutdown().await?;Implementations§
Source§impl Supervisor
impl Supervisor
Sourcepub fn new(config: SupervisorConfig) -> Supervisor
pub fn new(config: SupervisorConfig) -> Supervisor
Create a new supervisor with the given configuration.
Sourcepub fn with_defaults() -> Supervisor
pub fn with_defaults() -> Supervisor
Create a new supervisor with default configuration.
Sourcepub fn cancel_token(&self) -> &CancellationToken
pub fn cancel_token(&self) -> &CancellationToken
Get a reference to the supervisor’s cancellation token.
Useful for external code that needs to observe or trigger shutdown.
Sourcepub fn metrics(&self) -> &Arc<SupervisorMetrics> ⓘ
pub fn metrics(&self) -> &Arc<SupervisorMetrics> ⓘ
Get a reference to the supervisor’s metrics.
Sourcepub async fn lifecycle_snapshots(&self) -> Vec<ServiceLifecycleSnapshot>
pub async fn lifecycle_snapshots(&self) -> Vec<ServiceLifecycleSnapshot>
Snapshot all service lifecycles.
Sourcepub async fn service_lifecycle(
&self,
name: &str,
) -> Option<ServiceLifecycleSnapshot>
pub async fn service_lifecycle( &self, name: &str, ) -> Option<ServiceLifecycleSnapshot>
Lifecycle snapshot for a specific service by name.
Sourcepub async fn service_count(&self) -> usize
pub async fn service_count(&self) -> usize
Number of services currently tracked (alive + terminated).
Sourcepub fn trigger_shutdown(&self)
pub fn trigger_shutdown(&self)
Trigger a graceful shutdown of all managed services.
Sourcepub fn is_shutting_down(&self) -> bool
pub fn is_shutting_down(&self) -> bool
Returns true if the supervisor’s shutdown has been triggered.
Sourcepub fn spawn_service(&self, service: Box<dyn TradingService>)
pub fn spawn_service(&self, service: Box<dyn TradingService>)
Spawn a service with default options.
Sourcepub fn spawn_service_with_options(
&self,
service: Box<dyn TradingService>,
options: SpawnOptions,
)
pub fn spawn_service_with_options( &self, service: Box<dyn TradingService>, options: SpawnOptions, )
Spawn a service with custom per-service options.
Sourcepub async fn wait_for_drain(&self)
pub async fn wait_for_drain(&self)
Close the tracker and wait for all tasks to complete, with timeout.
Call after triggering shutdown (or after run_until_shutdown returns)
to ensure all tasks have drained.
Sourcepub async fn run_until_shutdown(&self) -> Result<(), Error>
pub async fn run_until_shutdown(&self) -> Result<(), Error>
Run the supervisor until a shutdown signal is received.
If install_signal_handler is true (the default), listens for
Ctrl-C / SIGTERM and triggers shutdown. Returns after all services
have drained (or the shutdown timeout has elapsed).