upstart, stabilizing

This commit is contained in:
rjawor 2015-12-16 09:48:15 +01:00
parent 2724696875
commit c7459af387
9 changed files with 122 additions and 22 deletions

6
.gitignore vendored
View File

@ -1,5 +1,6 @@
build/ build/
logs/concordia-server.log logs/concordia-server.log
logs/pgbouncer.log
concordia.cfg concordia.cfg
concordia-server/config.hpp concordia-server/config.hpp
index/ index/
@ -9,6 +10,7 @@ scripts/stop.sh
scripts/restart.sh scripts/restart.sh
scripts/watchdog.sh scripts/watchdog.sh
scripts/watchdog.log scripts/watchdog.log
db/pgbouncer.log
db/pgbouncer.pid db/pgbouncer.pid
db/pgbouncer.ini
upstart/concordia-server.conf
upstart/pgbouncer.conf

View File

@ -44,6 +44,21 @@ configure_file (
"${concordia-server_SOURCE_DIR}/scripts/watchdog.sh" "${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(CONFIG_FILE_PATH "${concordia-server_SOURCE_DIR}/concordia.cfg")
set(LOG_FILE_PATH "${concordia-server_SOURCE_DIR}/logs/concordia-server.log") set(LOG_FILE_PATH "${concordia-server_SOURCE_DIR}/logs/concordia-server.log")

View File

@ -32,9 +32,8 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
rapidjson::Writer<rapidjson::StringBuffer> jsonWriter(outputJson); rapidjson::Writer<rapidjson::StringBuffer> jsonWriter(outputJson);
std::stringstream outputString; std::stringstream outputString;
try {
outputString << "Access-Control-Allow-Origin: *\r\n";
outputString << "Content-type: application/json\r\n\r\n"; outputString << "Content-type: application/json\r\n\r\n";
try {
rapidjson::Document d; rapidjson::Document d;
bool hasError = d.Parse(requestString.c_str()).HasParseError(); bool hasError = d.Parse(requestString.c_str()).HasParseError();
@ -44,16 +43,16 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
", description: " << GetParseError_En(d.GetParseError()); ", description: " << GetParseError_En(d.GetParseError());
JsonGenerator::signalError(jsonWriter, errorstream.str()); JsonGenerator::signalError(jsonWriter, errorstream.str());
} else { // json parsed } else { // json parsed
std::string operation = d[OPERATION_PARAM].GetString(); std::string operation = _getStringParameter(d, OPERATION_PARAM);
if (operation == ADD_SENTENCE_OP) { if (operation == ADD_SENTENCE_OP) {
std::string sourceSentence = d[SOURCE_SENTENCE_PARAM].GetString(); std::string sourceSentence = _getStringParameter(d, SOURCE_SENTENCE_PARAM);
std::string targetSentence = d[TARGET_SENTENCE_PARAM].GetString(); std::string targetSentence = _getStringParameter(d, TARGET_SENTENCE_PARAM);
int tmId = d[TM_ID_PARAM].GetInt(); int tmId = _getIntParameter(d, TM_ID_PARAM);
_indexController->addSentence(jsonWriter, sourceSentence, targetSentence, tmId); _indexController->addSentence(jsonWriter, sourceSentence, targetSentence, tmId);
} else if (operation == ADD_SENTENCES_OP) { } else if (operation == ADD_SENTENCES_OP) {
std::vector<std::string> sourceSentences; std::vector<std::string> sourceSentences;
std::vector<std::string> targetSentences; std::vector<std::string> targetSentences;
int tmId = d[TM_ID_PARAM].GetInt(); int tmId = _getIntParameter(d, TM_ID_PARAM);
// loading data from json // loading data from json
const rapidjson::Value & sentencesArray = d[SENTENCES_PARAM]; const rapidjson::Value & sentencesArray = d[SENTENCES_PARAM];
Logger::log("addSentences"); Logger::log("addSentences");
@ -89,21 +88,21 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
} }
_indexController->addAlignedSentences(jsonWriter, sourceSentences, targetSentences, tmId); _indexController->addAlignedSentences(jsonWriter, sourceSentences, targetSentences, tmId);
} else if (operation == REFRESH_INDEX_OP) { } else if (operation == REFRESH_INDEX_OP) {
int tmId = d[TM_ID_PARAM].GetInt(); int tmId = _getIntParameter(d, TM_ID_PARAM);
_indexController->refreshIndexFromRAM(jsonWriter, tmId); _indexController->refreshIndexFromRAM(jsonWriter, tmId);
} else if (operation == SIMPLE_SEARCH_OP) { } else if (operation == SIMPLE_SEARCH_OP) {
std::string pattern = d[PATTERN_PARAM].GetString(); std::string pattern = _getStringParameter(d, PATTERN_PARAM);
int tmId = d[TM_ID_PARAM].GetInt(); int tmId = _getIntParameter(d, TM_ID_PARAM);
_searcherController->simpleSearch(jsonWriter, pattern, tmId); _searcherController->simpleSearch(jsonWriter, pattern, tmId);
} else if (operation == CONCORDIA_SEARCH_OP) { } else if (operation == CONCORDIA_SEARCH_OP) {
std::string pattern = d[PATTERN_PARAM].GetString(); std::string pattern = _getStringParameter(d, PATTERN_PARAM);
int tmId = d[TM_ID_PARAM].GetInt(); int tmId = _getIntParameter(d, TM_ID_PARAM);
Logger::logString("concordia search pattern", pattern); Logger::logString("concordia search pattern", pattern);
_searcherController->concordiaSearch(jsonWriter, pattern, tmId); _searcherController->concordiaSearch(jsonWriter, pattern, tmId);
} else if (operation == ADD_TM_OP) { } else if (operation == ADD_TM_OP) {
int sourceLangId = d[SOURCE_LANG_PARAM].GetInt(); int sourceLangId = _getIntParameter(d, SOURCE_LANG_PARAM);
int targetLangId = d[TARGET_LANG_PARAM].GetInt(); int targetLangId = _getIntParameter(d, TARGET_LANG_PARAM);
std::string name = d[NAME_PARAM].GetString(); std::string name = _getStringParameter(d, NAME_PARAM);
int newId = _tmDAO.addTm(sourceLangId, targetLangId, name); int newId = _tmDAO.addTm(sourceLangId, targetLangId, name);
_addTm(newId); _addTm(newId);
@ -115,7 +114,7 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
jsonWriter.EndObject(); jsonWriter.EndObject();
} else { } 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) { void ConcordiaServer::_addTm(int tmId) {
std::stringstream indexPath; std::stringstream indexPath;
indexPath << INDEX_DIRECTORY << "/tm_" << tmId; indexPath << INDEX_DIRECTORY << "/tm_" << tmId;

View File

@ -30,6 +30,10 @@ public:
std::string handleRequest(std::string & requestString); std::string handleRequest(std::string & requestString);
private: 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); void _addTm(int tmId);
std::string _configFilePath; std::string _configFilePath;

View File

@ -131,6 +131,9 @@ void IndexController::refreshIndexFromRAM(rapidjson::Writer<rapidjson::StringBuf
std::vector<AlignedUnit> IndexController::_getAlignedUnits(const std::vector<std::string> & sourceSentences, std::vector<AlignedUnit> IndexController::_getAlignedUnits(const std::vector<std::string> & sourceSentences,
const std::vector<std::string> & targetSentences) { const std::vector<std::string> & targetSentences) {
//TODO //TODO
std::vector<AlignedUnit> result;
return result;
} }

View File

@ -5,7 +5,7 @@ concordia_server = host=127.0.0.1 port=5432 dbname=concordia_server
listen_port = 6543 listen_port = 6543
listen_addr = * listen_addr = *
auth_type = md5 auth_type = md5
auth_file = users.txt auth_file = /home/rafalj/projects/concordia-server/db/users.txt
logfile = pgbouncer.log logfile = /home/rafalj/projects/concordia-server/logs/pgbouncer.log
pidfile = pgbouncer.pid pidfile = /home/rafalj/projects/concordia-server/db/pgbouncer.pid
admin_users = concordia admin_users = concordia

11
db/pgbouncer.ini.in Normal file
View File

@ -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

View File

@ -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

View File

@ -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