feature(signal): Add option to disable signal handling (#742)

This commit is contained in:
Bertrand Darbon 2021-03-11 15:03:02 +01:00 committed by GitHub
parent 1901801d59
commit bcd8e27a36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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)

View File

@ -463,7 +463,10 @@ void HttpAppFrameworkImpl::run()
getLoop()->resetAfterFork();
#endif
}
signal(SIGTERM, TERMFunction);
if (handleSigterm_)
{
signal(SIGTERM, TERMFunction);
}
// set logger
if (!logPath_.empty())
{

View File

@ -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<int64_t> connectionNum_{0};
bool runAsDaemon_{false};
bool handleSigterm_{true};
bool relaunchOnError_{false};
std::string logPath_{""};
std::string logfileBaseName_{""};