now winix waites for the database to be ready (when the operating system starts)

git-svn-id: svn://ttmath.org/publicrep/winix/trunk@638 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2010-08-14 16:23:18 +00:00
parent 1e7d297c0e
commit b63ac98f40
5 changed files with 51 additions and 15 deletions

View File

@ -131,6 +131,9 @@ return true;
bool App::Init()
{
db.Init(config.db_database, config.db_user, config.db_pass);
db.WaitForConnection();
if( !CreateFCGISocket() )
return false;
@ -149,9 +152,10 @@ bool App::Init()
templates.ReadTemplates();
templates.CreateFunctions();
session_manager.LoadSessions();
plugin.Call(WINIX_PLUGIN_INIT);
return true;
}

View File

@ -51,9 +51,6 @@ void Db::Connect()
pg_conn = PQconnectdb(buf.str().c_str());
if( pg_conn )
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
// warning! pg_conn can be not null but there cannnot be a connection established
// use PQstatus(pg_conn) to check whether the connection works fine
}
@ -79,7 +76,8 @@ void Db::Close()
}
void Db::AssertConnection()
bool Db::AssertConnection(bool put_log, bool throw_if_no_connection)
{
bool was_connection = true;
@ -92,7 +90,9 @@ bool was_connection = true;
else
if( PQstatus(pg_conn) != CONNECTION_OK )
{
log << log2 << "Db: connection to the database is lost, trying to recover" << logend;
if( put_log )
log << log2 << "Db: connection to the database is lost, trying to recover" << logend;
was_connection = false;
PQreset(pg_conn);
}
@ -100,19 +100,46 @@ bool was_connection = true;
if( pg_conn && PQstatus(pg_conn) == CONNECTION_OK )
{
if( was_connection == false )
if( put_log && was_connection == false )
{
log << log2 << "Db: Connection to the database works fine" << logend;
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
}
SetDbParameters();
return true;
}
else
{
log << log1 << "Db: Connection to db server cannot be established" << logend;
throw Error(WINIX_ERR_DB_FATAL_ERROR_DURING_CONNECTING);
if( put_log )
log << log1 << "Db: Connection to db server cannot be established" << logend;
if( throw_if_no_connection )
throw Error(WINIX_ERR_DB_FATAL_ERROR_DURING_CONNECTING);
return false;
}
}
void Db::WaitForConnection()
{
if( !pg_conn || PQstatus(pg_conn) != CONNECTION_OK )
{
log << log3 << "Db: waiting for the db to be ready...." << logend;
while( !AssertConnection(false, false) )
sleep(5);
log << log3 << "Db: connection to the db works fine" << logend;
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
}
}
std::string Db::Escape(const std::string & s)
{

View File

@ -43,6 +43,8 @@ public:
// !! GetFile i GetDir beda uzywac GetItem
void Init(const std::string & database, const std::string & user, const std::string & pass);
void WaitForConnection();
bool CheckUser(const std::string & login, const std::string & password, long & user_id);
Error AddUser(User & user, const std::string & password);
@ -203,7 +205,8 @@ protected:
void Close();
void AssertConnection();
bool AssertConnection(bool put_log = true, bool throw_if_no_connection = true);
std::string Escape(const std::string & s);
std::string Escape(const char * s);
PGresult * AssertQuery(const std::string & q);

View File

@ -44,6 +44,11 @@
// PluginInfo::l1 is the dir id
#define WINIX_DIR_PREPARE_TO_REMOVE 3007
// winix is initialized,
// now you can initialize your plugin
#define WINIX_PLUGIN_INIT 3008
#endif

View File

@ -84,19 +84,16 @@ int main(int argv, char ** argc)
app.stdout_is_closed = true;
}
log.Init(app.config.log_level, app.config.log_file, app.config.log_stdout, app.config.log_request);
nlog.Init(app.config.log_level, app.config.log_notify_file, false, 1);
app.db.Init(app.config.db_database, app.config.db_user, app.config.db_pass);
// app.config.base_server can be changed (stripped from 'http://' or a last slash)
// it is done when the config is read
log << log3 << "base_server: " << app.config.base_server << logend;
// load plugins before loading sessions - session_manager.LoadSessions()
// because some of the plugins can init your own session dates
// because some of the plugins can init its own sessions dates
plugin.LoadPlugins(app.config.plugin_file);
//plugin.Call(WINIX_PLUGIN_INIT);
if( !app.Init() )
return 1;