Skip to content
Snippets Groups Projects
Commit 147d495b authored by Fabian Tërnava's avatar Fabian Tërnava
Browse files

check if mongodb is running. If not return empty results

parent 1e43c02c
No related branches found
No related tags found
No related merge requests found
......@@ -10,20 +10,47 @@
namespace armarx::armem::ltm
{
bool Memory::checkConnection() const
{
// Check connection:
if (!MongoDBConnectionManager::ConnectionIsValid(dbsettings))
{
ARMARX_WARNING << deactivateSpam("ConnectionIsNotValid") << "The connection to mongocxx for memory '" << name() << "' is not valid. Settings are: " << dbsettings.toString();
return false;
}
ARMARX_INFO << "Checking connection";
return true;
}
wm::Memory Memory::convert() const
{
if (!checkConnection())
{
wm::Memory m(id());
return m;
}
ARMARX_INFO << "Converting with connection to: " << dbsettings.toString();
TIMING_START(LTM_Convert);
wm::Memory m(id());
for (const auto& [_, s] : _container)
{
m.addCoreSegment(s.convert());
}
TIMING_END(LTM_Convert);
return m;
}
void Memory::reload()
{
if (!checkConnection())
{
return;
}
ARMARX_INFO << "(Re)Establishing connection to: " << dbsettings.toString();
_container.clear();
......@@ -60,6 +87,11 @@ namespace armarx::armem::ltm
void Memory::append(const wm::Memory& m)
{
if (!checkConnection())
{
return;
}
ARMARX_INFO << "Merge memory with name '" << m.name() << "' into the LTM with name '" << name() << "'";
TIMING_START(LTM_Append);
......
......@@ -65,6 +65,8 @@ namespace armarx::armem::ltm
other.dbsettings = dbsettings;
}
private:
bool checkConnection() const;
public:
MongoDBConnectionManager::MongoDBSettings dbsettings;
......
......@@ -5,13 +5,19 @@ namespace armarx::armem::ltm
bool MongoDBConnectionManager::initialized = false;
std::map<std::string, mongocxx::client> MongoDBConnectionManager::Connections = {};
mongocxx::client& MongoDBConnectionManager::EstablishConnection(const MongoDBSettings& settings)
void MongoDBConnectionManager::initialize_if()
{
if (!initialized)
{
initialized = true;
mongocxx::instance instance{}; // This should be done only once.
}
}
mongocxx::client& MongoDBConnectionManager::EstablishConnection(const MongoDBSettings& settings)
{
initialize_if();
const auto uri_str = settings.uri();
const auto& it = Connections.find(uri_str);
......@@ -27,4 +33,44 @@ namespace armarx::armem::ltm
return it->second;
}
}
bool MongoDBConnectionManager::ConnectionIsValid(const MongoDBSettings& settings, bool force)
{
initialize_if();
try
{
if (!force)
{
const auto uri_str = settings.uri();
const auto& it = Connections.find(uri_str);
if (it != Connections.end())
{
auto admin = it->second["admin"];
auto result = admin.run_command(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("isMaster", 1)));
return true;
}
}
const auto uri_str = settings.uri();
mongocxx::uri uri(uri_str);
auto client = mongocxx::client(uri);
auto admin = client["admin"];
auto result = admin.run_command(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("isMaster", 1)));
return true;
}
catch (const std::exception& xcp)
{
return false;
}
}
bool MongoDBConnectionManager::ConnectionExists(const MongoDBSettings& settings)
{
initialize_if();
const auto uri_str = settings.uri();
const auto& it = Connections.find(uri_str);
return it != Connections.end();
}
}
......@@ -4,6 +4,7 @@
#include <vector>
#include <map>
#include <memory>
#include <iostream>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
......@@ -35,7 +36,7 @@ namespace armarx::armem::ltm
struct MongoDBSettings
{
std::string host = "localhost";
unsigned int port = 27017;
unsigned int port = 25270;
std::string user = "";
std::string password = "";
std::string database = "Test";
......@@ -59,6 +60,11 @@ namespace armarx::armem::ltm
};
static mongocxx::client& EstablishConnection(const MongoDBSettings& settings);
static bool ConnectionIsValid(const MongoDBSettings& settings, bool force = false);
static bool ConnectionExists(const MongoDBSettings& settings);
private:
static void initialize_if();
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment