Add a method for the TERM signal handling (#475)

This commit is contained in:
An Tao 2020-06-14 17:36:10 +08:00 committed by GitHub
parent 4ebb72b0cf
commit 2457f9b413
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 3 deletions

View File

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

View File

@ -105,15 +105,17 @@ std::string getVersion()
{
return DROGON_VERSION;
}
std::string getGitCommit()
{
return DROGON_VERSION_SHA1;
}
HttpResponsePtr defaultErrorHandler(HttpStatusCode code)
{
return std::make_shared<HttpResponseImpl>(code, CT_TEXT_HTML);
}
} // namespace drogon
static void godaemon(void)
{
printf("Initializing daemon mode\n");
@ -151,6 +153,18 @@ static void godaemon(void)
return;
}
static void TERMFunction(int sig)
{
if (sig == SIGTERM)
{
LOG_WARN << "SIGTERM signal received.";
HttpAppFrameworkImpl::instance().getTermSignalHandler()();
}
}
} // namespace drogon
HttpAppFrameworkImpl::~HttpAppFrameworkImpl() noexcept
{
// Destroy the following objects before the loop destruction
@ -413,7 +427,7 @@ void HttpAppFrameworkImpl::run()
getLoop()->resetAfterFork();
#endif
}
signal(SIGTERM, TERMFunction);
// set logger
if (!logPath_.empty())
{

View File

@ -333,6 +333,16 @@ class HttpAppFrameworkImpl : public HttpAppFramework
{
return homePageFile_;
}
virtual HttpAppFramework &setTermSignalHandler(
const std::function<void()> &handler) override
{
termSignalHandler_ = handler;
return *this;
}
const std::function<void()> &getTermSignalHandler() const
{
return termSignalHandler_;
}
size_t getClientMaxBodySize() const
{
return clientMaxBodySize_;
@ -536,6 +546,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
size_t clientMaxMemoryBodySize_{64 * 1024};
size_t clientMaxWebSocketMessageSize_{128 * 1024};
std::string homePageFile_{"index.html"};
std::function<void()> termSignalHandler_{[]() { app().quit(); }};
std::unique_ptr<SessionManager> sessionManagerPtr_;
Json::Value jsonConfig_;
HttpResponsePtr custom404_;

@ -1 +1 @@
Subproject commit 55232baacebb77036babfeb8c652420032241b01
Subproject commit 3692af0ca6282854c26cf3deb9fe31d78e2434b0