From af31304c550ab36c11e65758aee9e4b6a4a72cfd Mon Sep 17 00:00:00 2001 From: Ben Oukhanov Date: Sat, 6 May 2023 20:23:36 +0300 Subject: [PATCH] fix(auth-service): ping to establish db connection --- .../MongoRepository/MongoDatabaseProvider.cs | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/auth-service/Authenticator.Infrastructure/MongoRepository/MongoDatabaseProvider.cs b/src/auth-service/Authenticator.Infrastructure/MongoRepository/MongoDatabaseProvider.cs index 1b984d8ec..ff40741a5 100644 --- a/src/auth-service/Authenticator.Infrastructure/MongoRepository/MongoDatabaseProvider.cs +++ b/src/auth-service/Authenticator.Infrastructure/MongoRepository/MongoDatabaseProvider.cs @@ -1,4 +1,6 @@ -using MongoDB.Driver; +using System; +using MongoDB.Driver; +using MongoDB.Bson; namespace Authenticator.Infrastructure.MongoRepository { @@ -13,8 +15,25 @@ public MongoDatabaseProvider(string url) { var mongoUrl = new MongoUrl(url); var mongoClient = new MongoClient(mongoUrl); - database = mongoClient.GetDatabase(mongoUrl.DatabaseName); + + Ping(url); + } + + /// + /// Force the connection to be established. + /// + private void Ping(string url) + { + try + { + var pingCommand = new BsonDocument("ping", 1); + database.RunCommand(pingCommand); + } + catch (Exception exception) + { + throw new Exception($"Failed to connect to MongoDB server at {url}. {exception.Message}", exception); + } } public IMongoDatabase Provide()