Skip to content

Commit

Permalink
Some work on exitmessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersbakken committed Feb 21, 2014
1 parent 222c3e4 commit d7a6dfa
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
31 changes: 27 additions & 4 deletions src/RClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ struct Option opts[] = {

{ None, 0, 0, 0, "" },
{ None, 0, 0, 0, "Rdm:" },
{ QuitRdm, "quit-rdm", 'q', no_argument, "Tell server to shut down." },
{ QuitRdm, "quit-rdm", 'q', optional_argument, "Tell server to shut down. If arg is broadcast:10 tell all rdm on the farm to quit with exit code 10." },
{ ConnectTimeout, "connect-timeout", 0, required_argument, "Timeout for connecting to rdm in ms (default " STR(DEFAULT_CONNECT_TIMEOUT) ")." },

{ None, 0, 0, 0, "" },
Expand Down Expand Up @@ -789,9 +789,32 @@ bool RClient::parse(int &argc, char **argv)
case XmlDiagnostics:
addLog(RTags::CompilationErrorXml);
break;
case QuitRdm:
addQuery(QueryMessage::Shutdown);
break;
case QuitRdm: {
const char *arg;
if (optarg) {
arg = optarg;
} else if (optind < argc && argv[optind][0] != '-') {
arg = argv[optind++];
}
if (arg) {
if (strncmp(arg, "broadcast:", 10)) {
fprintf(stderr, "Invalid argument to -q\n");
return 1;
}
bool ok;
const int exit = String(arg + 10).toLongLong(&ok);
if (!ok) {
fprintf(stderr, "Invalid argument to -q\n");
return 1;
}
String query;
Serializer serializer(query);
serializer << exit;
addQuery(QueryMessage::Shutdown, query);
} else {
addQuery(QueryMessage::Shutdown);
}
break; }
case DeleteProject:
addQuery(QueryMessage::DeleteProject, optarg);
break;
Expand Down
3 changes: 2 additions & 1 deletion src/RTagsMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class RTagsMessage : public Message
JobAnnouncementId,
ProxyJobAnnouncementId,
ClientMessageId,
ClientConnectedId
ClientConnectedId,
ExitMessageId
};

RTagsMessage(uint8_t id) : Message(id) {}
Expand Down
28 changes: 26 additions & 2 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "VisitFileResponseMessage.h"
#include "JobAnnouncementMessage.h"
#include "ProxyJobAnnouncementMessage.h"
#include "ExitMessage.h"
#include "ClientMessage.h"
#include "ClientConnectedMessage.h"
#include "PreprocessJob.h"
Expand Down Expand Up @@ -112,11 +113,12 @@ static const bool debugMulti = getenv("RDM_DEBUG_MULTI");
Server *Server::sInstance = 0;
Server::Server()
: mVerbose(false), mCurrentFileId(0), mThreadPool(0), mServerConnection(0), mHostName(Rct::hostName()),
mCompletionThread(0), mFirstRemote(0), mLastRemote(0), mAnnounced(false), mWorkPending(false)
mCompletionThread(0), mFirstRemote(0), mLastRemote(0), mAnnounced(false), mWorkPending(false), mExitCode(0)
{
Messages::registerMessage<JobRequestMessage>();
Messages::registerMessage<JobResponseMessage>();
Messages::registerMessage<ClientConnectedMessage>();
Messages::registerMessage<ExitMessage>();

assert(!sInstance);
sInstance = this;
Expand Down Expand Up @@ -383,6 +385,9 @@ void Server::onNewMessage(Message *message, Connection *connection)
error() << m->raw();
handleLogOutputMessage(static_cast<const LogOutputMessage&>(*m), connection);
break;
case ExitMessage::MessageId:
handleExitMessage(static_cast<const ExitMessage&>(*m));
break;
case VisitFileMessage::MessageId:
handleVisitFileMessage(static_cast<const VisitFileMessage&>(*m), connection);
break;
Expand Down Expand Up @@ -464,6 +469,17 @@ void Server::handleCompileMessage(CompileMessage &message, Connection *conn)
index(message.arguments(), message.workingDirectory(), message.projects());
}

void Server::handleExitMessage(const ExitMessage &message)
{
mExitCode = message.exitCode();
for (auto client : mClients) {
client->send(message);
}

EventLoop::eventLoop()->registerTimer(std::bind(&EventLoop::quit, EventLoop::eventLoop()),
1000, Timer::SingleShot);
}

void Server::handleLogOutputMessage(const LogOutputMessage &message, Connection *conn)
{
new LogObject(conn, message.level());
Expand Down Expand Up @@ -1330,7 +1346,15 @@ void Server::shutdown(const QueryMessage &query, Connection *conn)
if (it.second)
it.second->unload();
}
EventLoop::eventLoop()->quit();
if (!query.query().isEmpty()) {
int exitCode;
Deserializer deserializer(query.query());
deserializer >> exitCode;
ExitMessage msg(exitCode);
handleExitMessage(msg);
} else {
EventLoop::eventLoop()->quit();
}
conn->write("Shutting down");
conn->finish();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class JobResponseMessage;
class JobAnnouncementMessage;
class ProxyJobAnnouncementMessage;
class ClientConnectedMessage;
class ExitMessage;
class ClientMessage;
class CompletionThread;
class PreprocessJob;
Expand Down Expand Up @@ -114,6 +115,7 @@ class Server
void stopServers();
int mongooseStatistics(struct mg_connection *conn);
void dumpJobs(Connection *conn);
int exitCode() const { return mExitCode; }
private:
bool selectProject(const Match &match, Connection *conn, unsigned int queryFlags);
bool updateProject(const List<String> &projects, unsigned int queryFlags);
Expand All @@ -129,6 +131,7 @@ class Server
void onNewMessage(Message *message, Connection *conn);
void onConnectionDisconnected(Connection *o);
void clearProjects();
void handleExitMessage(const ExitMessage &message);
void handleCompileMessage(CompileMessage &message, Connection *conn);
void handleIndexerMessage(const IndexerMessage &message, Connection *conn);
void handleQueryMessage(const QueryMessage &message, Connection *conn);
Expand Down Expand Up @@ -233,6 +236,7 @@ class Server
bool mAnnounced;
Hash<std::string, Remote *> mRemotes;
bool mWorkPending;
int mExitCode;
};

#endif
2 changes: 1 addition & 1 deletion src/rdm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,5 +596,5 @@ int main(int argc, char** argv)

const unsigned int ret = loop->exec();
cleanupLogging();
return ret == EventLoop::Success ? 0 : 1;
return ret == EventLoop::Success ? server->exitCode() : 1;
}

0 comments on commit d7a6dfa

Please sign in to comment.