acl/app/master/tools/master_monitor/master_service.cpp

165 lines
3.1 KiB
C++
Raw Normal View History

#include "stdafx.h"
2018-01-29 17:24:54 +08:00
#include <signal.h>
2018-01-24 16:44:38 +08:00
#include "json/service_struct.h"
#include "json/service_struct.gson.h"
2018-01-29 17:24:54 +08:00
#include "ipc_monitor.h"
2018-01-24 16:44:38 +08:00
#include "tools.h"
#include "guard_report.h"
#include "daemon/json/serialize.h"
#include "master_service.h"
char *var_cfg_guard_manager;
acl::master_str_tbl var_conf_str_tab[] = {
2018-05-07 09:49:50 +08:00
{ "guard_manager", "master.domain:8390", &var_cfg_guard_manager },
2018-01-24 16:44:38 +08:00
{ 0, 0, 0 }
};
acl::master_bool_tbl var_conf_bool_tab[] = {
{ 0, 0, 0 }
};
2018-01-29 17:24:54 +08:00
static int var_cfg_conn_timeout;
static int var_cfg_rw_timeout;
static int var_cfg_connection_idle;
2018-01-24 16:44:38 +08:00
acl::master_int_tbl var_conf_int_tab[] = {
2018-01-29 17:24:54 +08:00
{ "conn_timeout", 30, &var_cfg_conn_timeout, 0, 0 },
{ "rw_timeout", 30, &var_cfg_rw_timeout, 0, 0 },
{ "connetion_idle", 30, &var_cfg_connection_idle, 0, 0 },
2018-01-24 16:44:38 +08:00
{ 0, 0 , 0 , 0, 0 }
};
acl::master_int64_tbl var_conf_int64_tab[] = {
{ 0, 0 , 0 , 0, 0 }
};
//////////////////////////////////////////////////////////////////////////
master_service::master_service(void)
2018-01-29 17:24:54 +08:00
: service_exit_(false)
, monitor_(NULL)
2018-01-24 16:44:38 +08:00
{
}
master_service::~master_service(void)
{
}
void master_service::handle(const acl::string& data)
{
service_dead_res_t res;
acl::url_coder coder;
coder.decode(data);
const char* ptr = coder.get("path");
2018-01-24 16:44:38 +08:00
if (ptr && *ptr)
res.path = ptr;
2018-01-31 14:28:45 +08:00
ptr = coder.get("conf");
if (ptr && *ptr)
res.conf = ptr;
2018-01-24 16:44:38 +08:00
ptr = coder.get("pid");
if (ptr && *ptr)
{
int pid = atoi(ptr);
res.pid = pid;
}
else
res.pid = -1;
ptr = coder.get("ver");
if (ptr && *ptr)
{
if (strcasecmp(ptr, "-v") == 0 && !res.path.empty())
tools::get_version(res.path, res.version);
else
res.version = ptr;
}
2018-01-24 16:44:38 +08:00
ptr = coder.get("rcpt");
if (ptr && *ptr)
res.rcpt = ptr;
ptr = coder.get("info");
if (ptr && *ptr)
res.info = ptr;
res.status = 200;
res.cmd = "service_dead";
acl::string body;
serialize<service_dead_res_t>(res, body);
2018-01-29 17:24:54 +08:00
guard_report report(var_cfg_guard_manager, ipc_, 10, 10);
2018-01-24 16:44:38 +08:00
report.report(body);
logger("report=|%s|", body.c_str());
2018-01-24 16:44:38 +08:00
}
void master_service::on_accept(acl::socket_stream& conn)
{
conn.set_rw_timeout(10);
acl::string buf;
while (true)
{
if (conn.gets(buf) == false)
break;
if (!buf.empty())
handle(buf);
}
}
void master_service::proc_pre_jail(void)
{
2018-01-30 10:02:27 +08:00
//logger(">>>proc_pre_jail<<<");
2018-01-24 16:44:38 +08:00
}
2018-01-30 10:02:27 +08:00
void master_service::proc_on_listen(acl::server_socket&)
2018-01-24 16:44:38 +08:00
{
2018-01-30 10:02:27 +08:00
//logger(">>>listen %s ok<<<", ss.get_addr());
2018-01-24 16:44:38 +08:00
}
void master_service::proc_on_init(void)
{
2018-01-30 10:02:27 +08:00
//logger(">>>proc_on_init<<<");
2018-01-29 17:24:54 +08:00
ipc_.set_limit(0)
.set_idle(30)
.set_conn_timeout(var_cfg_conn_timeout)
.set_rw_timeout(var_cfg_rw_timeout);
monitor_ = new ipc_monitor(ipc_, var_cfg_connection_idle, service_exit_);
monitor_->set_detachable(false);
monitor_->start();
}
static void wait_timeout(int)
{
exit (1);
2018-01-24 16:44:38 +08:00
}
void master_service::proc_on_exit(void)
{
2018-01-30 10:02:27 +08:00
//logger(">>>proc_on_exit<<<");
2018-01-29 17:24:54 +08:00
service_exit_ = true;
signal(SIGALRM, wait_timeout);
alarm(10);
monitor_->wait();
delete monitor_;
2018-01-24 16:44:38 +08:00
}
bool master_service::proc_on_sighup(acl::string&)
{
logger(">>>proc_on_sighup<<<");
return true;
}