mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-07 05:24:46 +01:00
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:
parent
8f863f0369
commit
9c7213e42b
2 changed files with 37 additions and 1 deletions
|
@ -44,6 +44,7 @@
|
||||||
static DBMgr* s_instance = NULL;
|
static DBMgr* s_instance = NULL;
|
||||||
|
|
||||||
#define MAX_NUM_PLAYERS 4
|
#define MAX_NUM_PLAYERS 4
|
||||||
|
#define MAX_WAIT_SECONDS (5*60) // five minutes
|
||||||
|
|
||||||
static int here_less_seed( const char* seeds, int perDeviceSum,
|
static int here_less_seed( const char* seeds, int perDeviceSum,
|
||||||
unsigned short seed );
|
unsigned short seed );
|
||||||
|
@ -747,6 +748,34 @@ DBMgr::KillGame( const char* const connName, int hid )
|
||||||
execSql( query );
|
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
|
void
|
||||||
DBMgr::ClearCIDs( void )
|
DBMgr::ClearCIDs( void )
|
||||||
{
|
{
|
||||||
|
@ -1330,7 +1359,12 @@ DBMgr::getThreadConn( void )
|
||||||
params.catf( "port = %d ", port );
|
params.catf( "port = %d ", port );
|
||||||
|
|
||||||
conn = PQconnectdb( params.c_str() );
|
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;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,6 +63,8 @@ class DBMgr {
|
||||||
|
|
||||||
~DBMgr();
|
~DBMgr();
|
||||||
|
|
||||||
|
void WaitDBConn( void );
|
||||||
|
|
||||||
void ClearCIDs( void );
|
void ClearCIDs( void );
|
||||||
|
|
||||||
void AddNew( const char* cookie, const char* connName, CookieID cid,
|
void AddNew( const char* cookie, const char* connName, CookieID cid,
|
||||||
|
|
Loading…
Reference in a new issue