Starting/stopping instances from web-interface

This commit is contained in:
Jan Kaluza 2012-08-29 10:39:58 +02:00
parent 254143e164
commit f9a49506a1
4 changed files with 35 additions and 1 deletions

View file

@ -185,7 +185,7 @@ int main(int argc, char **argv)
elapsed = 1000000.0 * (td_end.tv_sec -td_start.tv_sec); \
elapsed += (td_end.tv_usec - td_start.tv_usec); \
elapsed = elapsed / 1000 / 1000; \
std::cout << "Response received after " << (elapsed) << " seconds\n";
// std::cout << "Response received after " << (elapsed) << " seconds\n";
}
}
}

View file

@ -176,10 +176,12 @@ void start_instances(ManagerConfig *config, const std::string &_jid) {
int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
if (pid == 0) {
response = "Starting " + itr->path().string() + ": OK\n";
std::cout << "Starting " << itr->path() << ": OK\n";
exec_(spectrum2_binary, itr->path().string(), vhost);
}
else {
response = "Starting " + itr->path().string() + ": Already started (PID=" + boost::lexical_cast<std::string>(pid) + ")\n";
std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
}
}
@ -232,6 +234,7 @@ void stop_instances(ManagerConfig *config, const std::string &_jid) {
int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
if (pid) {
response ="Stopping " + itr->path().string() + ": ";
std::cout << "Stopping " << itr->path() << ": ";
kill(pid, SIGTERM);
@ -243,13 +246,16 @@ void stop_instances(ManagerConfig *config, const std::string &_jid) {
count--;
}
if (count == 0) {
response += "ERROR (timeout)\n";
std::cout << " ERROR (timeout)\n";
}
else {
response += "OK\n";
std::cout << " OK\n";
}
}
else {
response = "Stopping " + itr->path().string() + ": Not running\n";
std::cout << "Stopping " << itr->path() << ": Not running\n";
}
}

View file

@ -306,6 +306,28 @@ void Server::serve_login(struct mg_connection *conn, const struct mg_request_inf
print_html(conn, request_info, html);
}
void Server::serve_start(struct mg_connection *conn, const struct mg_request_info *request_info) {
std::string html= get_header() ;
char jid[255];
get_qsvar(request_info, "jid", jid, sizeof(jid));
start_instances(m_config, jid);
html += "<b>" + get_response() + "</b>";
html += "</body></html>";
print_html(conn, request_info, html);
}
void Server::serve_stop(struct mg_connection *conn, const struct mg_request_info *request_info) {
std::string html= get_header();
char jid[255];
get_qsvar(request_info, "jid", jid, sizeof(jid));
stop_instances(m_config, jid);
html += "<b>" + get_response() + "</b>";
html += "</body></html>";
print_html(conn, request_info, html);
}
void Server::serve_root(struct mg_connection *conn, const struct mg_request_info *request_info) {
std::vector<std::string> list = show_list(m_config, false);
std::string html= get_header() + "<h2>List of instances</h2><table><tr><th>JID<th>Status</th><th>Command</th></tr>";
@ -348,6 +370,10 @@ void *Server::event_handler(enum mg_event event, struct mg_connection *conn) {
serve_login(conn, request_info);
} else if (strcmp(request_info->uri, "/") == 0) {
serve_root(conn, request_info);
} else if (strcmp(request_info->uri, "/start") == 0) {
serve_start(conn, request_info);
} else if (strcmp(request_info->uri, "/stop") == 0) {
serve_stop(conn, request_info);
} else {
// No suitable handler found, mark as not processed. Mongoose will
// try to serve the request.

View file

@ -53,6 +53,8 @@ class Server {
private:
void serve_login(struct mg_connection *conn, const struct mg_request_info *request_info);
void serve_root(struct mg_connection *conn, const struct mg_request_info *request_info);
void serve_start(struct mg_connection *conn, const struct mg_request_info *request_info);
void serve_stop(struct mg_connection *conn, const struct mg_request_info *request_info);
void print_html(struct mg_connection *conn, const struct mg_request_info *request_info, const std::string &html);
private: