pub struct ServiceManager { /* private fields */ }Expand description
Centralized manager for service lifecycles.
The ServiceManager is responsible for:
- Registering services with configuration
- Starting services in order
- Monitoring service health
- Stopping services gracefully (in reverse order)
- Broadcasting shutdown signals
§Lifecycle
- Register Phase -
register()services with config (callsService::init()) - Start Phase -
start_all()starts all services (callsService::start()) - Running Phase - Services execute, manager monitors health
- Stop Phase -
stop_all()stops services in reverse order (callsService::stop())
§Example
let mut manager = ServiceManager::new();
// Register services (init phase)
manager.register::<MyService>(config).await?;
// Start all services
manager.start_all().await?;
// Check health
let health = manager.health_check_all().await;
for (name, status) in health {
println!("{}: {:?}", name, status);
}
// Graceful shutdown
manager.stop_all().await?;Implementations§
Source§impl ServiceManager
impl ServiceManager
Sourcepub async fn register<S>(&mut self, config: S::Config) -> Result<()>where
S: Service + 'static,
pub async fn register<S>(&mut self, config: S::Config) -> Result<()>where
S: Service + 'static,
Register a service with configuration.
Calls Service::init() to initialize the service, then stores it
for later startup. Services are started in the order they are registered.
§Arguments
config- Service-specific configuration
§Returns
Ok(())- Service registered successfullyErr(_)- Service initialization failed
§Example
let mut manager = ServiceManager::new();
// Register HTTP API
manager.register::<HttpApiService>(HttpApiConfig {
host: "0.0.0.0".to_string(),
port: 8080,
}).await?;
// Register database
manager.register::<DatabaseService>(DatabaseConfig {
url: "postgresql://localhost/mecha10".to_string(),
max_connections: 20,
}).await?;Sourcepub async fn start_all(&mut self) -> Result<()>
pub async fn start_all(&mut self) -> Result<()>
Start all registered services.
Services are started in the order they were registered. If any service fails to start, the error is returned and remaining services are not started.
§Returns
Ok(())- All services started successfullyErr(_)- A service failed to start
§Example
let mut manager = ServiceManager::new();
// ... register services ...
// Start all services
manager.start_all().await?;
info!("All services started successfully");Sourcepub async fn stop_all(&mut self) -> Result<()>
pub async fn stop_all(&mut self) -> Result<()>
Stop all services gracefully.
Services are stopped in reverse order of registration. This ensures that dependent services are stopped first (e.g., stop API server before database).
Each service’s stop() method is called, even if previous services failed
to stop. All errors are logged, and the first error encountered is returned.
§Returns
Ok(())- All services stopped successfullyErr(_)- At least one service failed to stop
§Example
// ... services running ...
// Graceful shutdown
tokio::signal::ctrl_c().await?;
manager.stop_all().await?;Sourcepub async fn health_check_all(&self) -> Vec<(String, HealthStatus)>
pub async fn health_check_all(&self) -> Vec<(String, HealthStatus)>
Check health of all services.
Calls health_check() on each registered service and returns a vector
of (name, status) tuples.
§Returns
Vec<(String, HealthStatus)>- Health status for each service
§Example
let health = manager.health_check_all().await;
for (name, status) in health {
if !status.healthy {
warn!("Service '{}' is unhealthy: {:?}", name, status.message);
} else {
info!("Service '{}' is healthy", name);
}
}Sourcepub fn shutdown_signal(&self) -> Receiver<()>
pub fn shutdown_signal(&self) -> Receiver<()>
Get a broadcast receiver for shutdown signals.
Services can use this to listen for shutdown signals and stop gracefully.
§Returns
broadcast::Receiver<()>- Shutdown signal receiver
§Example
let manager = ServiceManager::new();
let mut shutdown_rx = manager.shutdown_signal();
tokio::spawn(async move {
shutdown_rx.recv().await.ok();
// Service cleanup...
});Sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Trigger shutdown by broadcasting to all listeners.
This sends a signal to all shutdown_signal() receivers. Services
should listen to this signal to stop gracefully.
Note: This does NOT call stop_all(). You must call stop_all()
separately to actually stop services.
§Example
let mut manager = ServiceManager::new();
// Trigger shutdown signal
manager.shutdown();
// Then stop all services
manager.stop_all().await.ok();Sourcepub fn service_count(&self) -> usize
pub fn service_count(&self) -> usize
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ServiceManager
impl !RefUnwindSafe for ServiceManager
impl Send for ServiceManager
impl Sync for ServiceManager
impl Unpin for ServiceManager
impl !UnwindSafe for ServiceManager
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more