allow to specify how many times we can try to connect to the database at startup
add config options: db_startup_connection_max_attempts - default 0 (infinite) db_startup_connection_attempt_delay - delay in seconds between attempts (default 5) BREAKING CHANGE: WINIX_PLUGIN_INIT plugin message requires to set result status, you have to set the result status to true (env.res) if your plugin was initialized correctly, otherwise winix will not start
This commit is contained in:
@@ -130,26 +130,63 @@ void DbConn::Connect()
|
||||
}
|
||||
|
||||
|
||||
void DbConn::LogNoConnection(size_t attempts)
|
||||
{
|
||||
log << log2 << "Db: connection to the database cannot be established";
|
||||
log << ", (" << attempts << " attempt(s))" << logend;
|
||||
log << logsave;
|
||||
}
|
||||
|
||||
|
||||
void DbConn::LogConnectionSocket()
|
||||
{
|
||||
log << log2 << "Db: connection to the database works fine" << logend;
|
||||
log << log3 << "Db: connection socket: " << PQsocket(pg_conn) << logend;
|
||||
log << logsave;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbConn::WaitForConnection()
|
||||
bool DbConn::WaitForConnection(size_t attempts_max, size_t attempt_delay)
|
||||
{
|
||||
size_t attempts = 0;
|
||||
bool attempts_exceeded = false;
|
||||
|
||||
if( attempt_delay == 0 )
|
||||
attempt_delay = 1;
|
||||
|
||||
if( attempt_delay > 120 )
|
||||
attempt_delay = 120;
|
||||
|
||||
if( !pg_conn || PQstatus(pg_conn) != CONNECTION_OK )
|
||||
{
|
||||
log << log3 << "Db: waiting for the db to be ready...." << logend << logsave;
|
||||
|
||||
while( !AssertConnection(false, false) )
|
||||
sleep(5);
|
||||
while( !attempts_exceeded && !AssertConnection(false, false) )
|
||||
{
|
||||
if( attempts_max != 0 )
|
||||
{
|
||||
attempts += 1;
|
||||
attempts_exceeded = (attempts >= attempts_max);
|
||||
}
|
||||
|
||||
if( !attempts_exceeded )
|
||||
{
|
||||
sleep(attempt_delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( attempts_exceeded )
|
||||
{
|
||||
LogNoConnection(attempts);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogConnectionSocket();
|
||||
}
|
||||
|
||||
return !attempts_exceeded;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user