Make it possible to launch relay early in machine boot cycle before

postgres is running (e.g. from a @reboot shortcut in a crontab) by
having it wait, sleeping periodically, until a connection is
available. Requires new flag be passed into main.
This commit is contained in:
Relay User 2015-07-11 07:03:22 -07:00
parent 8f863f0369
commit 9c7213e42b
2 changed files with 37 additions and 1 deletions

View file

@ -44,6 +44,7 @@
static DBMgr* s_instance = NULL;
#define MAX_NUM_PLAYERS 4
#define MAX_WAIT_SECONDS (5*60) // five minutes
static int here_less_seed( const char* seeds, int perDeviceSum,
unsigned short seed );
@ -747,6 +748,34 @@ DBMgr::KillGame( const char* const connName, int hid )
execSql( query );
}
void
DBMgr::WaitDBConn( void )
{
int nSeconds = 0;
int toSleep = 1;
for ( ; ; ) {
PGconn* conn = DBMgr::getThreadConn();
if ( !!conn ) {
ConnStatusType status = PQstatus( conn );
if ( CONNECTION_OK == status ) {
break;
}
}
toSleep *= 2;
if ( toSleep > MAX_WAIT_SECONDS ) {
toSleep = MAX_WAIT_SECONDS;
}
(void)sleep( toSleep );
nSeconds += toSleep;
logf( XW_LOGERROR, "%s: waiting for postgres; %d seconds so far", __func__,
nSeconds );
}
logf( XW_LOGERROR, "%s() done", __func__ );
}
void
DBMgr::ClearCIDs( void )
{
@ -1330,7 +1359,12 @@ DBMgr::getThreadConn( void )
params.catf( "port = %d ", port );
conn = PQconnectdb( params.c_str() );
pthread_setspecific( m_conn_key, conn );
if ( CONNECTION_OK == PQstatus( conn ) ) {
pthread_setspecific( m_conn_key, conn );
} else {
PQfinish( conn );
conn = NULL;
}
}
return conn;
}

View file

@ -63,6 +63,8 @@ class DBMgr {
~DBMgr();
void WaitDBConn( void );
void ClearCIDs( void );
void AddNew( const char* cookie, const char* connName, CookieID cid,