DB: Make connection initialization more predictable and avoid double-initialization

Remove calls to zmDBConnect from various places to avoid possible side-effects/double initialization.
The function should be called once from the main thread of the daemon.

Also split config loading into 2 steps: static and DB config loading. Load the static config before zmDBConnect is called so it has a chance to succeed.
This commit is contained in:
Peter Keresztes Schmidt 2021-02-07 13:44:41 +01:00
parent ace93e2422
commit aec4dbc6ff
7 changed files with 21 additions and 10 deletions

View File

@ -30,8 +30,7 @@
// set the relevant ENV vars because the logger gets it's setting from the
// config.
void zmLoadConfig() {
void zmLoadStaticConfig() {
// Process name, value pairs from the main config file first
process_configfile(ZM_CONFIG);
@ -58,9 +57,11 @@ void zmLoadConfig() {
globfree(&pglob);
closedir(configSubFolder);
}
}
if ( !zmDbConnect() ) {
Fatal("Can't connect to db. Can't continue.");
void zmLoadDBConfig() {
if ( !zmDbConnected ) {
Fatal("Not connected to the database. Can't continue.");
}
config.Load();
config.Assign();

View File

@ -53,7 +53,8 @@
#define ZM_SAMPLE_RATE int(1000000/ZM_MAX_FPS) // A general nyquist sample frequency for delays etc
#define ZM_SUSPENDED_RATE int(1000000/4) // A slower rate for when disabled etc
extern void zmLoadConfig();
void zmLoadStaticConfig();
void zmLoadDBConfig();
extern void process_configfile(char const *configFile);

View File

@ -43,6 +43,8 @@ class zmDbRow {
extern MYSQL dbconn;
extern RecursiveMutex db_mutex;
extern bool zmDbConnected;
bool zmDbConnect();
void zmDbClose();

View File

@ -342,7 +342,7 @@ Logger::Level Logger::databaseLevel(Logger::Level databaseLevel) {
databaseLevel = limit(databaseLevel);
if ( mDatabaseLevel != databaseLevel ) {
if ( (databaseLevel > NOLOG) && (mDatabaseLevel <= NOLOG) ) { // <= NOLOG would be NOOPT
if ( !zmDbConnect() ) {
if ( !zmDbConnected ) {
databaseLevel = NOLOG;
}
} // end if ( databaseLevel > NOLOG && mDatabaseLevel <= NOLOG )

View File

@ -181,7 +181,9 @@ int main(int argc, char *argv[]) {
}
logInit(log_id_string);
zmLoadConfig();
zmLoadStaticConfig();
zmDbConnect();
zmLoadDBConfig();
logInit(log_id_string);
hwcaps_detect();

View File

@ -81,9 +81,12 @@ int main(int argc, const char *argv[], char **envp) {
nph = true;
}
zmLoadConfig();
char log_id_string[32] = "zms";
logInit(log_id_string);
zmLoadStaticConfig();
zmDbConnect();
zmLoadDBConfig();
for (char **env = envp; *env != 0; env++) {
char *thisEnv = *env;
Debug(1, "env: %s", thisEnv);

View File

@ -417,8 +417,10 @@ int main(int argc, char *argv[]) {
}
//printf( "Monitor %d, Function %d\n", mon_id, function );
zmLoadConfig();
logInit("zmu");
zmLoadStaticConfig();
zmDbConnect();
zmLoadDBConfig();
logInit("zmu");
zmSetDefaultTermHandler();