From 471a8b77355a560c1ba2fe7fa6d8e879f3456558 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Sat, 9 Apr 2022 20:37:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=90=E8=BF=9B=E7=A8=8B=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=97=B6=EF=BC=8C=E4=B8=8D=E6=96=AD=E9=87=8D?= =?UTF-8?q?=E8=AF=95=EF=BC=8C=E9=81=BF=E5=85=8D=E9=87=8D=E5=90=AF=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=20(#1545)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update main.cpp * 双重保险, 避免重启失败 业务繁忙的服务器当子进程崩溃后, 虽然延时了3秒, 但是如果服务器负载很高, 或者开启了coredump 会导致拉起子进程时, 仍旧端口占用导致子进程重启失败而直接退出主进程. 因此, 这里做了双重保险, 当子进程拉起时如果端口占用, 那么子进程会继续重试. --- server/System.cpp | 14 ++++++++++++-- server/main.cpp | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/server/System.cpp b/server/System.cpp index 200a0ea5..f42a4a13 100644 --- a/server/System.cpp +++ b/server/System.cpp @@ -84,6 +84,7 @@ static void sig_crash(int sig) { void System::startDaemon() { #ifndef _WIN32 static pid_t pid; + static bool is_restart = false; do{ pid = fork(); if(pid == -1){ @@ -100,17 +101,26 @@ void System::startDaemon() { //父进程,监视子进程是否退出 DebugL << "启动子进程:" << pid; + signal(SIGUSR1, [](int) { + if (!is_restart){ + DebugL << "子进程启动错误, 即将退出"; + kill(pid, SIGINT); + exit(0); + }else{ + WarnL << "尝试再次重启子进程"; + } + }); signal(SIGINT, [](int) { WarnL << "收到主动退出信号,关闭父进程与子进程"; kill(pid,SIGINT); exit(0); }); - do{ int status = 0; if(waitpid(pid, &status, 0) >= 0) { WarnL << "子进程退出"; - //休眠3秒再启动子进程 + //休眠1秒再启动子进程 + is_restart = true; sleep(3); break; } diff --git a/server/main.cpp b/server/main.cpp index 1b8d1c5e..6227feb0 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -318,7 +318,7 @@ int start_main(int argc,char *argv[]) { sleep(1); #if !defined(_WIN32) if (pid != getpid()) { - kill(pid, SIGINT); + kill(pid, SIGUSR1); } #endif return -1;