Fix signal handle (#1106)

This commit is contained in:
NitroMelon 2021-12-05 18:43:49 +08:00 committed by GitHub
parent 6a55a3aa64
commit 2a484536d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -1108,6 +1108,19 @@ class DROGON_EXPORT HttpAppFramework : public trantor::NonCopyable
virtual HttpAppFramework &setTermSignalHandler( virtual HttpAppFramework &setTermSignalHandler(
const std::function<void()> &handler) = 0; const std::function<void()> &handler) = 0;
/**
* @brief Set the INT Signal Handler. This method provides a way to users
* for exiting program gracefully. When the INT signal is received after
* app().run() is called, the handler is invoked. Drogon uses a default
* signal handler for the INT signal, which calls the 'app().quit()' method
* when the INT signal is received.
*
* @param handler
* @return HttpAppFramework&
*/
virtual HttpAppFramework &setIntSignalHandler(
const std::function<void()> &handler) = 0;
/// Get homepage, default is "index.html" /// Get homepage, default is "index.html"
/** /**
* @note * @note

View File

@ -189,7 +189,7 @@ static void TERMFunction(int sig)
else if (sig == SIGINT) else if (sig == SIGINT)
{ {
LOG_WARN << "SIGINT signal received."; LOG_WARN << "SIGINT signal received.";
HttpAppFrameworkImpl::instance().getTermSignalHandler()(); HttpAppFrameworkImpl::instance().getIntSignalHandler()();
} }
} }
@ -504,6 +504,7 @@ void HttpAppFrameworkImpl::run()
struct sigaction sa; struct sigaction sa;
sa.sa_handler = TERMFunction; sa.sa_handler = TERMFunction;
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGINT, &sa, NULL) == -1) if (sigaction(SIGINT, &sa, NULL) == -1)
{ {
LOG_ERROR << "sigaction() failed, can't set SIGINT handler"; LOG_ERROR << "sigaction() failed, can't set SIGINT handler";

View File

@ -357,6 +357,16 @@ class HttpAppFrameworkImpl final : public HttpAppFramework
{ {
return termSignalHandler_; return termSignalHandler_;
} }
HttpAppFramework &setIntSignalHandler(
const std::function<void()> &handler) override
{
intSignalHandler_ = handler;
return *this;
}
const std::function<void()> &getIntSignalHandler() const
{
return intSignalHandler_;
}
HttpAppFramework &setImplicitPageEnable(bool useImplicitPage) override; HttpAppFramework &setImplicitPageEnable(bool useImplicitPage) override;
bool isImplicitPageEnabled() const override; bool isImplicitPageEnabled() const override;
HttpAppFramework &setImplicitPage( HttpAppFramework &setImplicitPage(
@ -622,6 +632,7 @@ class HttpAppFrameworkImpl final : public HttpAppFramework
size_t clientMaxWebSocketMessageSize_{128 * 1024}; size_t clientMaxWebSocketMessageSize_{128 * 1024};
std::string homePageFile_{"index.html"}; std::string homePageFile_{"index.html"};
std::function<void()> termSignalHandler_{[]() { app().quit(); }}; std::function<void()> termSignalHandler_{[]() { app().quit(); }};
std::function<void()> intSignalHandler_{[]() { app().quit(); }};
std::unique_ptr<SessionManager> sessionManagerPtr_; std::unique_ptr<SessionManager> sessionManagerPtr_;
Json::Value jsonConfig_; Json::Value jsonConfig_;
HttpResponsePtr custom404_; HttpResponsePtr custom404_;