From 71135aa0b27e1dde6b84f71f5afd196e5f9e92a9 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Sat, 12 Aug 2023 15:37:22 +0000 Subject: [PATCH] Move the database pool creation to the main function. Failure is fatal. --- .../license_server/src/main.rs | 59 +++++++++++-------- .../license_server/src/server.rs | 10 +--- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/rust/long_term_stats/license_server/src/main.rs b/src/rust/long_term_stats/license_server/src/main.rs index 8cfbb401..436989dc 100644 --- a/src/rust/long_term_stats/license_server/src/main.rs +++ b/src/rust/long_term_stats/license_server/src/main.rs @@ -1,35 +1,44 @@ use tracing_subscriber::fmt::format::FmtSpan; -mod server; mod pki; +mod server; fn set_console_logging() -> anyhow::Result<()> { - // install global collector configured based on RUST_LOG env var. - let subscriber = tracing_subscriber::fmt() - // Use a more compact, abbreviated log format - .compact() - // Display source code file paths - .with_file(true) - // Display source code line numbers - .with_line_number(true) - // Display the thread ID an event was recorded on - .with_thread_ids(true) - // Don't display the event's target (module path) - .with_target(false) - // Include per-span timings - .with_span_events(FmtSpan::CLOSE) - // Build the subscriber - .finish(); + // install global collector configured based on RUST_LOG env var. + let subscriber = tracing_subscriber::fmt() + // Use a more compact, abbreviated log format + .compact() + // Display source code file paths + .with_file(true) + // Display source code line numbers + .with_line_number(true) + // Display the thread ID an event was recorded on + .with_thread_ids(true) + // Don't display the event's target (module path) + .with_target(false) + // Include per-span timings + .with_span_events(FmtSpan::CLOSE) + // Build the subscriber + .finish(); - // Set the subscriber as the default - tracing::subscriber::set_global_default(subscriber)?; - Ok(()) + // Set the subscriber as the default + tracing::subscriber::set_global_default(subscriber)?; + Ok(()) } #[tokio::main] async fn main() -> anyhow::Result<()> { - // Start the logger - set_console_logging().unwrap(); + // Obtain the database pool + let pool = pgdb::get_connection_pool(10).await; + if pool.is_err() { + tracing::error!("Unable to connect to the database"); + tracing::error!("{pool:?}"); + return Err(anyhow::Error::msg("Unable to connect to the database")); + } + let pool = pool.unwrap(); - let _ = server::listen_accept().await; - Ok(()) -} \ No newline at end of file + // Start the logger + set_console_logging().unwrap(); + + let _ = server::listen_accept(pool.clone()).await; + Ok(()) +} diff --git a/src/rust/long_term_stats/license_server/src/server.rs b/src/rust/long_term_stats/license_server/src/server.rs index efee6911..35390b2d 100644 --- a/src/rust/long_term_stats/license_server/src/server.rs +++ b/src/rust/long_term_stats/license_server/src/server.rs @@ -10,18 +10,10 @@ use tokio::{ /// Entry point for the main license server system. /// Starts listening on port 9126 for license requests. -pub async fn listen_accept() -> anyhow::Result<()> { +pub async fn listen_accept(pool: Pool) -> anyhow::Result<()> { let listener = TcpListener::bind(":::9126").await?; tracing::info!("Listening on :::9126"); - let pool = pgdb::get_connection_pool(10).await; - if pool.is_err() { - tracing::error!("Unable to connect to the database"); - tracing::error!("{pool:?}"); - return Err(anyhow::Error::msg("Unable to connect to the database")); - } - let pool = pool.unwrap(); - loop { let (socket, address) = listener.accept().await?; tracing::info!("Connection from {address:?}");