From c7459af3879c068ba48463b1cf84117fce98833a Mon Sep 17 00:00:00 2001 From: rjawor Date: Wed, 16 Dec 2015 09:48:15 +0100 Subject: [PATCH] upstart, stabilizing --- .gitignore | 6 ++- CMakeLists.txt | 15 ++++++ concordia-server/concordia_server.cpp | 55 ++++++++++++++------ concordia-server/concordia_server.hpp | 4 ++ concordia-server/index_controller.cpp | 3 ++ db/pgbouncer.ini | 6 +-- db/pgbouncer.ini.in | 11 ++++ upstart/cmake_stubs/concordia-server.conf.in | 24 +++++++++ upstart/cmake_stubs/pgbouncer.conf.in | 20 +++++++ 9 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 db/pgbouncer.ini.in create mode 100644 upstart/cmake_stubs/concordia-server.conf.in create mode 100644 upstart/cmake_stubs/pgbouncer.conf.in diff --git a/.gitignore b/.gitignore index 3038d50..f46bc60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ build/ logs/concordia-server.log +logs/pgbouncer.log concordia.cfg concordia-server/config.hpp index/ @@ -9,6 +10,7 @@ scripts/stop.sh scripts/restart.sh scripts/watchdog.sh scripts/watchdog.log -db/pgbouncer.log db/pgbouncer.pid - +db/pgbouncer.ini +upstart/concordia-server.conf +upstart/pgbouncer.conf diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a8ce24..f34d500 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,21 @@ configure_file ( "${concordia-server_SOURCE_DIR}/scripts/watchdog.sh" ) +configure_file ( + "${concordia-server_SOURCE_DIR}/upstart/cmake_stubs/concordia-server.conf.in" + "${concordia-server_SOURCE_DIR}/upstart/concordia-server.conf" + ) + +configure_file ( + "${concordia-server_SOURCE_DIR}/upstart/cmake_stubs/pgbouncer.conf.in" + "${concordia-server_SOURCE_DIR}/upstart/pgbouncer.conf" + ) + +configure_file ( + "${concordia-server_SOURCE_DIR}/db/pgbouncer.ini.in" + "${concordia-server_SOURCE_DIR}/db/pgbouncer.ini" + ) + set(CONFIG_FILE_PATH "${concordia-server_SOURCE_DIR}/concordia.cfg") set(LOG_FILE_PATH "${concordia-server_SOURCE_DIR}/logs/concordia-server.log") diff --git a/concordia-server/concordia_server.cpp b/concordia-server/concordia_server.cpp index 1dec2ee..104637c 100644 --- a/concordia-server/concordia_server.cpp +++ b/concordia-server/concordia_server.cpp @@ -32,9 +32,8 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) { rapidjson::Writer jsonWriter(outputJson); std::stringstream outputString; + outputString << "Content-type: application/json\r\n\r\n"; try { - outputString << "Access-Control-Allow-Origin: *\r\n"; - outputString << "Content-type: application/json\r\n\r\n"; rapidjson::Document d; bool hasError = d.Parse(requestString.c_str()).HasParseError(); @@ -44,16 +43,16 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) { ", description: " << GetParseError_En(d.GetParseError()); JsonGenerator::signalError(jsonWriter, errorstream.str()); } else { // json parsed - std::string operation = d[OPERATION_PARAM].GetString(); - if (operation == ADD_SENTENCE_OP) { - std::string sourceSentence = d[SOURCE_SENTENCE_PARAM].GetString(); - std::string targetSentence = d[TARGET_SENTENCE_PARAM].GetString(); - int tmId = d[TM_ID_PARAM].GetInt(); + std::string operation = _getStringParameter(d, OPERATION_PARAM); + if (operation == ADD_SENTENCE_OP) { + std::string sourceSentence = _getStringParameter(d, SOURCE_SENTENCE_PARAM); + std::string targetSentence = _getStringParameter(d, TARGET_SENTENCE_PARAM); + int tmId = _getIntParameter(d, TM_ID_PARAM); _indexController->addSentence(jsonWriter, sourceSentence, targetSentence, tmId); } else if (operation == ADD_SENTENCES_OP) { std::vector sourceSentences; std::vector targetSentences; - int tmId = d[TM_ID_PARAM].GetInt(); + int tmId = _getIntParameter(d, TM_ID_PARAM); // loading data from json const rapidjson::Value & sentencesArray = d[SENTENCES_PARAM]; Logger::log("addSentences"); @@ -89,21 +88,21 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) { } _indexController->addAlignedSentences(jsonWriter, sourceSentences, targetSentences, tmId); } else if (operation == REFRESH_INDEX_OP) { - int tmId = d[TM_ID_PARAM].GetInt(); + int tmId = _getIntParameter(d, TM_ID_PARAM); _indexController->refreshIndexFromRAM(jsonWriter, tmId); } else if (operation == SIMPLE_SEARCH_OP) { - std::string pattern = d[PATTERN_PARAM].GetString(); - int tmId = d[TM_ID_PARAM].GetInt(); + std::string pattern = _getStringParameter(d, PATTERN_PARAM); + int tmId = _getIntParameter(d, TM_ID_PARAM); _searcherController->simpleSearch(jsonWriter, pattern, tmId); } else if (operation == CONCORDIA_SEARCH_OP) { - std::string pattern = d[PATTERN_PARAM].GetString(); - int tmId = d[TM_ID_PARAM].GetInt(); + std::string pattern = _getStringParameter(d, PATTERN_PARAM); + int tmId = _getIntParameter(d, TM_ID_PARAM); Logger::logString("concordia search pattern", pattern); _searcherController->concordiaSearch(jsonWriter, pattern, tmId); } else if (operation == ADD_TM_OP) { - int sourceLangId = d[SOURCE_LANG_PARAM].GetInt(); - int targetLangId = d[TARGET_LANG_PARAM].GetInt(); - std::string name = d[NAME_PARAM].GetString(); + int sourceLangId = _getIntParameter(d, SOURCE_LANG_PARAM); + int targetLangId = _getIntParameter(d, TARGET_LANG_PARAM); + std::string name = _getStringParameter(d, NAME_PARAM); int newId = _tmDAO.addTm(sourceLangId, targetLangId, name); _addTm(newId); @@ -115,7 +114,7 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) { jsonWriter.EndObject(); } else { - JsonGenerator::signalError(jsonWriter, "no such operation"); + JsonGenerator::signalError(jsonWriter, "no such operation: " + operation); } } @@ -131,6 +130,28 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) { } +std::string ConcordiaServer::_getStringParameter(rapidjson::Document & d, const char * name) + throw (ConcordiaException) { + rapidjson::Value::ConstMemberIterator itr = d.FindMember(name); + if (itr != d.MemberEnd()) { + std::string value = itr->value.GetString(); + return value; + } else { + throw ConcordiaException("missing parameter: " + std::string(name)); + } +} + +int ConcordiaServer::_getIntParameter(rapidjson::Document & d, const char * name) + throw (ConcordiaException) { + rapidjson::Value::ConstMemberIterator itr = d.FindMember(name); + if (itr != d.MemberEnd()) { + int value = itr->value.GetInt(); + return value; + } else { + throw ConcordiaException("missing parameter: " + std::string(name)); + } +} + void ConcordiaServer::_addTm(int tmId) { std::stringstream indexPath; indexPath << INDEX_DIRECTORY << "/tm_" << tmId; diff --git a/concordia-server/concordia_server.hpp b/concordia-server/concordia_server.hpp index edf57d9..4d43107 100644 --- a/concordia-server/concordia_server.hpp +++ b/concordia-server/concordia_server.hpp @@ -30,6 +30,10 @@ public: std::string handleRequest(std::string & requestString); private: + std::string _getStringParameter(rapidjson::Document & d, const char * name) throw (ConcordiaException); + + int _getIntParameter(rapidjson::Document & d, const char * name) throw (ConcordiaException); + void _addTm(int tmId); std::string _configFilePath; diff --git a/concordia-server/index_controller.cpp b/concordia-server/index_controller.cpp index 421be24..5079ca8 100644 --- a/concordia-server/index_controller.cpp +++ b/concordia-server/index_controller.cpp @@ -131,6 +131,9 @@ void IndexController::refreshIndexFromRAM(rapidjson::Writer IndexController::_getAlignedUnits(const std::vector & sourceSentences, const std::vector & targetSentences) { //TODO + std::vector result; + + return result; } diff --git a/db/pgbouncer.ini b/db/pgbouncer.ini index 486a715..5b87c93 100644 --- a/db/pgbouncer.ini +++ b/db/pgbouncer.ini @@ -5,7 +5,7 @@ concordia_server = host=127.0.0.1 port=5432 dbname=concordia_server listen_port = 6543 listen_addr = * auth_type = md5 -auth_file = users.txt -logfile = pgbouncer.log -pidfile = pgbouncer.pid +auth_file = /home/rafalj/projects/concordia-server/db/users.txt +logfile = /home/rafalj/projects/concordia-server/logs/pgbouncer.log +pidfile = /home/rafalj/projects/concordia-server/db/pgbouncer.pid admin_users = concordia diff --git a/db/pgbouncer.ini.in b/db/pgbouncer.ini.in new file mode 100644 index 0000000..476bcf1 --- /dev/null +++ b/db/pgbouncer.ini.in @@ -0,0 +1,11 @@ +[databases] +concordia_server = host=127.0.0.1 port=5432 dbname=concordia_server + +[pgbouncer] +listen_port = 6543 +listen_addr = * +auth_type = md5 +auth_file = @concordia-server_SOURCE_DIR@/db/users.txt +logfile = @concordia-server_SOURCE_DIR@/logs/pgbouncer.log +pidfile = @concordia-server_SOURCE_DIR@/db/pgbouncer.pid +admin_users = concordia diff --git a/upstart/cmake_stubs/concordia-server.conf.in b/upstart/cmake_stubs/concordia-server.conf.in new file mode 100644 index 0000000..389d724 --- /dev/null +++ b/upstart/cmake_stubs/concordia-server.conf.in @@ -0,0 +1,24 @@ +# concordia server +# +# This service maintains concordia server +# started until it is shut down again. + +description concordia-server + +# When to start the service +start on started pgbouncer + +# When to stop the service +stop on runlevel [016] + +# Automatically restart process if crashed +respawn + +# Essentially lets upstart know the process will detach itself to the background +expect fork + +# Start the process +script + exec spawn-fcgi -p 8000 -n @COMPILED_BINARIES_PATH@/concordia_server_process & +end script + diff --git a/upstart/cmake_stubs/pgbouncer.conf.in b/upstart/cmake_stubs/pgbouncer.conf.in new file mode 100644 index 0000000..3b40730 --- /dev/null +++ b/upstart/cmake_stubs/pgbouncer.conf.in @@ -0,0 +1,20 @@ +# PgBouncer Upstart script + +description "pgbouncer" + +start on (net-device-up + and local-filesystems + and runlevel [2345]) + +stop on runlevel [016] + +respawn + +respawn limit 10 30 + +expect fork + +script + exec pgbouncer -u rafalj @concordia-server_SOURCE_DIR@/db/pgbouncer.ini & + sleep 4 +end script