diff --git a/config.example.json b/config.example.json index 1f005cec..d3b3dac2 100644 --- a/config.example.json +++ b/config.example.json @@ -172,6 +172,8 @@ }, //run_as_daemon: False by default "run_as_daemon": false, + //handle_sig_term: True by default + "handle_sig_term": true, //relaunch_on_error: False by default, if true, the program will be restart by the parent after exiting; "relaunch_on_error": false, //use_sendfile: True by default, if true, the program diff --git a/drogon_ctl/templates/config.csp b/drogon_ctl/templates/config.csp index 0d1868b5..4307421b 100644 --- a/drogon_ctl/templates/config.csp +++ b/drogon_ctl/templates/config.csp @@ -172,6 +172,8 @@ }, //run_as_daemon: False by default "run_as_daemon": false, + //handle_sig_term: True by default + "handle_sig_term": true, //relaunch_on_error: False by default, if true, the program will be restart by the parent after exiting; "relaunch_on_error": false, //use_sendfile: True by default, if true, the program diff --git a/examples/simple_reverse_proxy/config.json b/examples/simple_reverse_proxy/config.json index f5071d06..47074733 100644 --- a/examples/simple_reverse_proxy/config.json +++ b/examples/simple_reverse_proxy/config.json @@ -87,6 +87,8 @@ }, //run_as_daemon: False by default "run_as_daemon": false, + //handle_sig_term: True by default + "handle_sig_term": true, //relaunch_on_error: False by default, if true, the program will be restart by the parent after exiting; "relaunch_on_error": false, //use_sendfile: True by default, if true, the program diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index ec84bfc9..cea36b84 100644 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -816,6 +816,16 @@ class HttpAppFramework : public trantor::NonCopyable */ virtual HttpAppFramework &enableRunAsDaemon() = 0; + /// Disable the handling of SIGTERM signal. + /** + * Enabled by default. + * + * @note + * This operation can be performed by an option in the configuration file. + * When disabled setTermSignalHandler() is useless + */ + virtual HttpAppFramework &disableSigtermHandling() = 0; + /// Make the application restart after crashing. /** * Disabled by default. diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index cb728aa6..6de316bf 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -393,6 +393,12 @@ static void loadApp(const Json::Value &app) { drogon::app().enableRunAsDaemon(); } + // handle SIGTERM + auto handleSigterm = app.get("handle_sig_term", true).asBool(); + if (!handleSigterm) + { + drogon::app().disableSigtermHandling(); + } // relaunch auto relaunch = app.get("relaunch_on_error", false).asBool(); if (relaunch) diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index de0856ad..d8fe3476 100644 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -463,7 +463,10 @@ void HttpAppFrameworkImpl::run() getLoop()->resetAfterFork(); #endif } - signal(SIGTERM, TERMFunction); + if (handleSigterm_) + { + signal(SIGTERM, TERMFunction); + } // set logger if (!logPath_.empty()) { diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 9f4e9ba8..908f0864 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -256,6 +256,11 @@ class HttpAppFrameworkImpl : public HttpAppFramework runAsDaemon_ = true; return *this; } + virtual HttpAppFramework &disableSigtermHandling() override + { + handleSigterm_ = false; + return *this; + } virtual HttpAppFramework &enableRelaunchOnError() override { relaunchOnError_ = true; @@ -583,6 +588,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework std::atomic connectionNum_{0}; bool runAsDaemon_{false}; + bool handleSigterm_{true}; bool relaunchOnError_{false}; std::string logPath_{""}; std::string logfileBaseName_{""};