debug: Add detailed logging and timeouts to MongoDB connection
This commit is contained in:
parent
7d83255051
commit
44a6f91505
1 changed files with 31 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
use mongodb::{Client, Database, Collection, bson::doc};
|
use mongodb::{Client, Database, Collection, bson::doc, options::ClientOptions};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use mongodb::bson::oid::ObjectId;
|
use mongodb::bson::oid::ObjectId;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
user::{User, UserRepository},
|
user::{User, UserRepository},
|
||||||
|
|
@ -17,9 +18,35 @@ pub struct MongoDb {
|
||||||
|
|
||||||
impl MongoDb {
|
impl MongoDb {
|
||||||
pub async fn new(uri: &str, db_name: &str) -> Result<Self> {
|
pub async fn new(uri: &str, db_name: &str) -> Result<Self> {
|
||||||
let client = Client::with_uri_str(uri).await?;
|
eprintln!("[MongoDB] Starting connection to: {}", uri);
|
||||||
|
|
||||||
|
// Parse the URI first
|
||||||
|
let mut client_options = ClientOptions::parse(uri).await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("[MongoDB] Failed to parse URI: {}", e);
|
||||||
|
anyhow::anyhow!("Failed to parse MongoDB URI: {}", e)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
eprintln!("[MongoDB] URI parsed successfully");
|
||||||
|
|
||||||
|
// Set connection timeout
|
||||||
|
client_options.server_selection_timeout = Some(Duration::from_secs(5));
|
||||||
|
client_options.connect_timeout = Some(Duration::from_secs(5));
|
||||||
|
|
||||||
|
eprintln!("[MongoDB] Connecting to server...");
|
||||||
|
|
||||||
|
let client = Client::with_options(client_options)
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("[MongoDB] Failed to create client: {}", e);
|
||||||
|
anyhow::anyhow!("Failed to create MongoDB client: {}", e)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
eprintln!("[MongoDB] Client created, selecting database...");
|
||||||
|
|
||||||
let database = client.database(db_name);
|
let database = client.database(db_name);
|
||||||
|
|
||||||
|
eprintln!("[MongoDB] Database selected: {}", db_name);
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
users: database.collection("users"),
|
users: database.collection("users"),
|
||||||
shares: database.collection("shares"),
|
shares: database.collection("shares"),
|
||||||
|
|
@ -28,7 +55,9 @@ impl MongoDb {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn health_check(&self) -> Result<String> {
|
pub async fn health_check(&self) -> Result<String> {
|
||||||
|
eprintln!("[MongoDB] Health check: pinging database...");
|
||||||
self.database.run_command(doc! { "ping": 1 }, None).await?;
|
self.database.run_command(doc! { "ping": 1 }, None).await?;
|
||||||
|
eprintln!("[MongoDB] Health check: OK");
|
||||||
Ok("OK".to_string())
|
Ok("OK".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue