diff --git a/core/app.cpp b/core/app.cpp index 17ad63e..805d6ad 100755 --- a/core/app.cpp +++ b/core/app.cpp @@ -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; } diff --git a/core/db.cpp b/core/db.cpp index c1c6409..a86899d 100755 --- a/core/db.cpp +++ b/core/db.cpp @@ -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) { diff --git a/core/db.h b/core/db.h index 738d910..a4244a1 100755 --- a/core/db.h +++ b/core/db.h @@ -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); diff --git a/core/pluginmsg.h b/core/pluginmsg.h index f3fdcac..fab0442 100755 --- a/core/pluginmsg.h +++ b/core/pluginmsg.h @@ -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 diff --git a/main/main.cpp b/main/main.cpp index 1faae4b..6497bf0 100755 --- a/main/main.cpp +++ b/main/main.cpp @@ -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;