I have two docker containers that run and communicate with each other. One is a mongo database and one runs c++ mongocxx client in Linux.
In my c++, I have this code which gets called very shortly after startup:
//queues is a map of database/collection to documents to add or update
void update_mongo(std::map<std::pair<std::string, std::string>,
std::vector<bsoncxx::document::value>>
queues)
{
mongocxx::instance instance;
char const* server_ip = std::getenv("MONGO_IP");
if (!server_ip)
{
server_ip = "localhost";
}
mongocxx::uri server_uri(
fmt::format("mongodb://{}:{}@{}/?directConnection=true",
USERNAME, PASSWORD, server_ip));
mongocxx::client client(server_uri);
while (true)
{
for (auto const& [database_collection, documents] : queues)
{
auto const& [database, collection] =
database_collection;
std::vector<bsoncxx::document::view> inserts;
std::vector<UpdateInfo> updates;
for (const auto& document : documents)
{
// update documents if they exist else add to inserts vector
}
if (!inserts.empty())
{
try
{
client.database(database)
.collection(collection)
.insert_many(inserts);
}
catch (std::exception const& e)
{
std::cerr <<
"Error inserting document into Mongo: " <<
std::string(e.what());
}
}
}
}
}
However, when I startup for the first 20 to 30 seconds I get very fast repeated logs that look like this:
Error inserting document into Mongo: No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on 'mongo:27017']: generic server error
Error inserting document into Mongo: No servers yet eligible for rescan: generic server error
Error inserting document into Mongo: No servers yet eligible for rescan: generic server error
Error inserting document into Mongo: No servers yet eligible for rescan: generic server error
Error inserting document into Mongo: No servers yet eligible for rescan: generic server error
etc...
Eventually, these errors go away.
On the mongo side, my supervisor.conf looks like this:
[program:mongod]
command=/usr/local/bin/docker-entrypoint.sh mongod --quiet --logpath %(ENV_MONGO_LOG)s --logRotate reopen --logappend --bind_ip_all --keyFile %(ENV_KEY_FILE)s
autostart=true
autorestart=true
And in the mongo container, I start it with this:
# Run logrotate on startup
logrotate -f /etc/logrotate.d/mongo
cp -f /etc/supervisor/conf.d/supervisor.base.conf /etc/supervisor/conf.d/supervisord.conf
if [ "$NO_REPLICA" = "1" ]; then
cat /etc/supervisor/conf.d/supervisor.no.replica.conf >> /etc/supervisor/conf.d/supervisord.conf
else
cat /etc/supervisor/conf.d/supervisor.replica.conf >> /etc/supervisor/conf.d/supervisord.conf
fi
/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
Any idea why it takes so long for the c++ to properly connect to mongo and insert documents? Should I just add a sleep for 30 seconds before actually making the connection?