Added switch --daemon: fork and run moses server in background. The parent won't exit until the child is ready to serve.

This commit is contained in:
Ulrich Germann 2015-12-07 16:01:12 +00:00
parent 3eccbeaf2b
commit 2be2481feb
4 changed files with 46 additions and 6 deletions

View File

@ -64,6 +64,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifdef HAVE_XMLRPC_C
#include "moses/server/Server.h"
#endif
#include <signal.h>
using namespace std;
using namespace Moses;
@ -140,11 +141,27 @@ void SimpleTranslationInterface::DestroyFeatureFunctionStatic()
Parameter params;
void
signal_handler(int signum)
{
if (signum == SIGALRM) {
exit(0); // that's what we expected from the child process after forking
}
else if (signum == SIGTERM || signum == SIGKILL) {
exit(0);
}
else {
std::cerr << "Unexpected signal " << signum << std::endl;
exit(signum);
}
}
//! run moses in server mode
int
run_as_server()
{
#ifdef HAVE_XMLRPC_C
kill(getppid(),SIGALRM);
MosesServer::Server server(params);
return server.run(); // actually: don't return. see Server::run()
#else
@ -322,17 +339,25 @@ int decoder_main(int argc, char const** argv)
if (!StaticData::LoadDataStatic(&params, argv[0]))
exit(1);
//
#if 1
pid_t pid;
if (params.GetParam("daemon")) {
pid = fork();
if (pid) { pause(); exit(0); } // parent process
}
#endif
// setting "-show-weights" -> just dump out weights and exit
if (params.isParamSpecified("show-weights")) {
ShowWeights();
exit(0);
}
if (params.GetParam("server"))
if (params.GetParam("server")) {
std::cerr << "RUN SERVER at pid " << pid << std::endl;
return run_as_server();
else
} else
return batch_run();
}
#ifdef NDEBUG
catch (const std::exception &e) {

View File

@ -218,6 +218,7 @@ Parameter::Parameter()
// server options
po::options_description server_opts("Moses Server Options");
AddParam(server_opts,"server", "Run moses as a translation server.");
AddParam(server_opts,"daemon", "Run moses as a translation server in the background.");
AddParam(server_opts,"server-port", "Port for moses server");
AddParam(server_opts,"server-log", "Log destination for moses server");
AddParam(server_opts,"serial", "Run server in serial mode, processing only one request at a time.");
@ -232,7 +233,6 @@ Parameter::Parameter()
"Max. number of seconds the server will keep a persistent connection alive.");
AddParam(server_opts,"server-timeout",
"Max. number of seconds the server will wait for a client to submit a request once a connection has been established.");
// session timeout and session cache size are for moses translation session handling
// they have nothing to do with the abyss server (but relate to the moses server)
AddParam(server_opts,"session-timeout",

View File

@ -1,5 +1,7 @@
// -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*-
#include "Server.h"
#include <sstream>
namespace MosesServer
{
Server::
@ -16,6 +18,12 @@ namespace MosesServer
m_registry.addMethod("close_session", m_close_session);
}
Server::
~Server()
{
unlink(m_pidfile.c_str());
}
int
Server::
run()
@ -32,7 +40,12 @@ namespace MosesServer
.keepaliveMaxConn(m_server_options.keepaliveMaxConn)
.timeout(m_server_options.timeout)
);
std::ostringstream pidfilename;
pidfilename << "/tmp/moses-server." << m_server_options.port << ".pid";
m_pidfile = pidfilename.str();
std::ofstream pidfile(m_pidfile.c_str());
pidfile << getpid() << std::endl;
pidfile.close();
XVERBOSE(1,"Listening on port " << m_server_options.port << std::endl);
if (m_server_options.is_serial)
{

View File

@ -16,6 +16,7 @@
#include "CloseSession.h"
#include "Session.h"
#include "moses/parameters/ServerOptions.h"
#include <string>
namespace MosesServer
{
@ -28,9 +29,10 @@ namespace MosesServer
xmlrpc_c::methodPtr const m_optimizer;
xmlrpc_c::methodPtr const m_translator;
xmlrpc_c::methodPtr const m_close_session;
std::string m_pidfile;
public:
Server(Moses::Parameter& params);
~Server();
int run();
void delete_session(uint64_t const session_id);