diff --git a/doc/releaseNotes.md b/doc/releaseNotes.md index 169f62c..f65795b 100644 --- a/doc/releaseNotes.md +++ b/doc/releaseNotes.md @@ -26,3 +26,4 @@ This file describes the main feature changes for each InfoLogger released versio ## next version - added header file InfoLoggerErrorCodes.h to reference centrally the definition of error code ranges reserved specifically for each o2 module, and possibly the individual description / documentation of each error code. +- fix in infoLoggerServer threads initialization. diff --git a/src/InfoLoggerDispatch.cxx b/src/InfoLoggerDispatch.cxx index fe4b181..a509be7 100644 --- a/src/InfoLoggerDispatch.cxx +++ b/src/InfoLoggerDispatch.cxx @@ -16,15 +16,15 @@ InfoLoggerDispatch::InfoLoggerDispatch(ConfigInfoLoggerServer* vConfig, SimpleLog* vLog) { - dispatchThread = std::make_unique(InfoLoggerDispatch::threadCallback, this); input = std::make_unique>>(vConfig->dbDispatchQueueSize); - dispatchThread->start(); if (vLog != NULL) { theLog = vLog; } else { theLog = &defaultLog; } theConfig = vConfig; + dispatchThread = std::make_unique(InfoLoggerDispatch::threadCallback, this, "InfoLoggerDispatch", 50000); + dispatchThread->start(); } InfoLoggerDispatch::~InfoLoggerDispatch() @@ -54,6 +54,10 @@ Thread::CallbackResult InfoLoggerDispatch::threadCallback(void* arg) return Thread::CallbackResult::Error; } + if (!dPtr->isReady) { + return Thread::CallbackResult::Idle; + } + int nMsgProcessed = 0; if (dPtr->customLoop()) { diff --git a/src/InfoLoggerDispatch.h b/src/InfoLoggerDispatch.h index 23b8143..88f4273 100644 --- a/src/InfoLoggerDispatch.h +++ b/src/InfoLoggerDispatch.h @@ -52,6 +52,9 @@ class InfoLoggerDispatch SimpleLog defaultLog; ConfigInfoLoggerServer* theConfig; + + bool isReady = false; // this flag must be set to true by derived instances when ready. + // customloop will not be called unless set. }; class InfoLoggerDispatchPrint : public InfoLoggerDispatch diff --git a/src/InfoLoggerDispatchBrowser.cxx b/src/InfoLoggerDispatchBrowser.cxx index ed11dd7..4428d12 100644 --- a/src/InfoLoggerDispatchBrowser.cxx +++ b/src/InfoLoggerDispatchBrowser.cxx @@ -81,6 +81,9 @@ InfoLoggerDispatchOnlineBrowser::InfoLoggerDispatchOnlineBrowser(ConfigInfoLogge throw __LINE__; } //theLog.info("%s() success\n",__FUNCTION__); + + // enable customloop callback + isReady = true; } InfoLoggerDispatchOnlineBrowser::~InfoLoggerDispatchOnlineBrowser() { diff --git a/src/InfoLoggerDispatchSQL.cxx b/src/InfoLoggerDispatchSQL.cxx index eebb2c2..3789e36 100644 --- a/src/InfoLoggerDispatchSQL.cxx +++ b/src/InfoLoggerDispatchSQL.cxx @@ -21,7 +21,7 @@ typedef bool my_bool; #endif // some constants -#define SQL_RETRY_CONNECT 1 // base retry time, will be sleeping up to 10x this value +#define SQL_RETRY_CONNECT 1 // SQL database connect retry time class InfoLoggerDispatchSQLImpl { @@ -103,14 +103,7 @@ void InfoLoggerDispatchSQLImpl::start() } // try to connect DB - int maxRetry = 10; - for (int n = 0; n < maxRetry; n++) { - InfoLoggerDispatchSQLImpl::connectDB(); - if (dbIsConnected) { - break; - } - sleep(SQL_RETRY_CONNECT); - } + // done automatically in customloop } InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, SimpleLog* log) : InfoLoggerDispatch(config, log) @@ -119,6 +112,9 @@ InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, Sim dPtr->theLog = log; dPtr->theConfig = config; dPtr->start(); + + // enable customloop callback + isReady = true; } void InfoLoggerDispatchSQLImpl::stop() @@ -152,7 +148,7 @@ int InfoLoggerDispatchSQLImpl::connectDB() time_t now = time(NULL); if (now < dbLastConnectTry + SQL_RETRY_CONNECT) { // wait before reconnecting - return 0; + return 1; } dbLastConnectTry = now; if (mysql_real_connect(&db, theConfig->dbHost.c_str(), theConfig->dbUser.c_str(), theConfig->dbPassword.c_str(), theConfig->dbName.c_str(), 0, NULL, 0)) { @@ -160,11 +156,11 @@ int InfoLoggerDispatchSQLImpl::connectDB() dbIsConnected = 1; dbConnectTrials = 0; } else { - if (dbConnectTrials == 1) { // the first attempt always fails, hide it + if (dbConnectTrials == 0) { // log only first attempt theLog->error("DB connection failed: %s", mysql_error(&db)); } dbConnectTrials++; - return 0; + return 1; } // create prepared insert statement @@ -209,8 +205,12 @@ int InfoLoggerDispatchSQLImpl::connectDB() int InfoLoggerDispatchSQLImpl::customLoop() { - - return connectDB(); + int err = connectDB(); + if (err) { + // temporization to avoid immediate retry + sleep(SQL_RETRY_CONNECT); + } + return err; } int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr lmsg)