mod grpc; mod http; mod telemetry; mod uuid; pub mod core { tonic::include_proto!("core"); } use crate::telemetry::history::TelemetryHistoryService; use crate::telemetry::management_service::TelemetryManagementService; use log::error; use std::sync::Arc; use tokio_util::sync::CancellationToken; pub async fn setup() -> anyhow::Result<()> { let cancellation_token = CancellationToken::new(); { let cancellation_token = cancellation_token.clone(); tokio::spawn(async move { let _ = tokio::signal::ctrl_c().await; cancellation_token.cancel(); }); } let tlm = Arc::new(TelemetryManagementService::new( TelemetryHistoryService::new()?, )?); let grpc_server = grpc::setup(cancellation_token.clone(), tlm.clone())?; let result = http::setup(cancellation_token.clone(), tlm.clone()).await; cancellation_token.cancel(); result?; // result is dropped grpc_server.await?; //grpc server is dropped drop(cancellation_token); // All cancellation tokens are now dropped // Perform cleanup functions - at this point all servers have stopped and we can be sure that cleaning things up is safe if let Some(tlm) = Arc::into_inner(tlm) { tlm.cleanup().await?; } else { error!("Could not clean up Telemetry Management Service. Arc not released.") } Ok(()) }