Core/MySQL: wait for the DB to be ready (#1286)

* Core/MySQL: wait for the DB to be ready
- Closes #1285

* Fix build on windows
This commit is contained in:
Francesco Borzì
2019-01-15 13:52:54 +01:00
committed by GitHub
+31 -3
View File
@@ -14,6 +14,8 @@
#include <mysql.h> #include <mysql.h>
#include <mysqld_error.h> #include <mysqld_error.h>
#include <errmsg.h> #include <errmsg.h>
#include <chrono>
#include <thread>
#include "MySQLConnection.h" #include "MySQLConnection.h"
#include "MySQLThreading.h" #include "MySQLThreading.h"
@@ -24,6 +26,9 @@
#include "Timer.h" #include "Timer.h"
#include "Log.h" #include "Log.h"
using namespace std::this_thread;
using namespace std::chrono;
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) : MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
m_reconnecting(false), m_reconnecting(false),
m_prepareError(false), m_prepareError(false),
@@ -107,8 +112,21 @@ bool MySQLConnection::Open()
} }
#endif #endif
m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(), // Possible improvement for future: make ATTEMPTS and SECONDS configurable values
m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0); uint32 const ATTEMPTS = 180;
uint32 const SECONDS = 10;
uint32 count = 0;
do {
m_Mysql = mysql_real_connect(
mysqlInit,
m_connectionInfo.host.c_str(),
m_connectionInfo.user.c_str(),
m_connectionInfo.password.c_str(),
m_connectionInfo.database.c_str(),
port,
unix_socket,
0);
if (m_Mysql) if (m_Mysql)
{ {
@@ -133,10 +151,20 @@ bool MySQLConnection::Open()
} }
else else
{ {
count++;
sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit)); sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
sLog->outError("Retrying in 10 seconds...\n\n");
sleep_for(seconds(SECONDS));
}
} while (!m_Mysql && count < ATTEMPTS);
sLog->outError(
"Could not connect to MySQL database at %s: %s after %d attempts\n",
m_connectionInfo.host.c_str(),
mysql_error(mysqlInit),
ATTEMPTS);
mysql_close(mysqlInit); mysql_close(mysqlInit);
return false; return false;
}
} }
bool MySQLConnection::PrepareStatements() bool MySQLConnection::PrepareStatements()