添加serverkeepalive hook接口

This commit is contained in:
monktan 2021-08-20 14:52:48 +08:00
parent 94a6dc66c9
commit a60ce1b1c9
2 changed files with 34 additions and 1 deletions

View File

@ -131,8 +131,12 @@ on_stream_none_reader=https://127.0.0.1/index/hook/on_stream_none_reader
on_stream_not_found=https://127.0.0.1/index/hook/on_stream_not_found on_stream_not_found=https://127.0.0.1/index/hook/on_stream_not_found
#服务器启动报告,可以用于服务器的崩溃重启事件监听 #服务器启动报告,可以用于服务器的崩溃重启事件监听
on_server_started=https://127.0.0.1/index/hook/on_server_started on_server_started=https://127.0.0.1/index/hook/on_server_started
#server保活上报
on_server_keepalive=https://127.0.0.1/index/hook/on_server_keepalive
#hook api最大等待回复时间单位秒 #hook api最大等待回复时间单位秒
timeoutSec=10 timeoutSec=10
#keepalive间隔
alive_interval=10.0
[http] [http]
#http服务器字符编码windows上默认gb2312 #http服务器字符编码windows上默认gb2312

View File

@ -42,7 +42,9 @@ const string kOnShellLogin = HOOK_FIELD"on_shell_login";
const string kOnStreamNoneReader = HOOK_FIELD"on_stream_none_reader"; const string kOnStreamNoneReader = HOOK_FIELD"on_stream_none_reader";
const string kOnHttpAccess = HOOK_FIELD"on_http_access"; const string kOnHttpAccess = HOOK_FIELD"on_http_access";
const string kOnServerStarted = HOOK_FIELD"on_server_started"; const string kOnServerStarted = HOOK_FIELD"on_server_started";
const string kOnServerKeepalive = HOOK_FIELD"on_server_keepalive";
const string kAdminParams = HOOK_FIELD"admin_params"; const string kAdminParams = HOOK_FIELD"admin_params";
const string kAliveInterval = HOOK_FIELD"alive_interval";
onceToken token([](){ onceToken token([](){
mINI::Instance()[kEnable] = false; mINI::Instance()[kEnable] = false;
@ -61,7 +63,9 @@ onceToken token([](){
mINI::Instance()[kOnStreamNoneReader] = ""; mINI::Instance()[kOnStreamNoneReader] = "";
mINI::Instance()[kOnHttpAccess] = ""; mINI::Instance()[kOnHttpAccess] = "";
mINI::Instance()[kOnServerStarted] = ""; mINI::Instance()[kOnServerStarted] = "";
mINI::Instance()[kOnServerKeepalive] = "";
mINI::Instance()[kAdminParams] = "secret=035c73f7-bb6b-4889-a715-d9eb2d1925cc"; mINI::Instance()[kAdminParams] = "secret=035c73f7-bb6b-4889-a715-d9eb2d1925cc";
mINI::Instance()[kAliveInterval] = 30.0;
},nullptr); },nullptr);
}//namespace Hook }//namespace Hook
@ -184,6 +188,28 @@ static void reportServerStarted(){
do_http_hook(hook_server_started,body, nullptr); do_http_hook(hook_server_started,body, nullptr);
} }
// 服务器定时保活定时器
Timer::Ptr g_keepalive_timer;
static void reportServerKeepalive() {
GET_CONFIG(bool,hook_enable,Hook::kEnable);
GET_CONFIG(string, hook_server_keepalive, Hook::kOnServerKeepalive);
if(!hook_enable || hook_server_keepalive.empty()){
return;
}
GET_CONFIG(float, alive_interval, Hook::kAliveInterval);
g_keepalive_timer = std::make_shared<Timer>(alive_interval, [&](){
ArgsType body;
body["data"]["MediaSource"] = (Json::UInt64)(ObjectStatistic<MediaSource>::count());
body["data"]["MultiMediaSourceMuxer"] = (Json::UInt64)(ObjectStatistic<MultiMediaSourceMuxer>::count());
//执行hook
do_http_hook(hook_server_keepalive, body, nullptr);
return true;
}, nullptr);
}
void installWebHook(){ void installWebHook(){
GET_CONFIG(bool,hook_enable,Hook::kEnable); GET_CONFIG(bool,hook_enable,Hook::kEnable);
GET_CONFIG(string,hook_adminparams,Hook::kAdminParams); GET_CONFIG(string,hook_adminparams,Hook::kAdminParams);
@ -484,8 +510,11 @@ void installWebHook(){
//汇报服务器重新启动 //汇报服务器重新启动
reportServerStarted(); reportServerStarted();
//定时上报保活
reportServerKeepalive();
} }
void unInstallWebHook(){ void unInstallWebHook(){
g_keepalive_timer.reset();
} }