diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index b23d6d39..b9126712 100644 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -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 &handler) = 0; + /// Get homepage, default is "index.html" /** * @note diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 8a6990f8..4b3944b8 100644 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -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(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()) { diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index afd5e4fc..069b0c73 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -333,6 +333,16 @@ class HttpAppFrameworkImpl : public HttpAppFramework { return homePageFile_; } + virtual HttpAppFramework &setTermSignalHandler( + const std::function &handler) override + { + termSignalHandler_ = handler; + return *this; + } + const std::function &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 termSignalHandler_{[]() { app().quit(); }}; std::unique_ptr sessionManagerPtr_; Json::Value jsonConfig_; HttpResponsePtr custom404_; diff --git a/trantor b/trantor index 55232baa..3692af0c 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit 55232baacebb77036babfeb8c652420032241b01 +Subproject commit 3692af0ca6282854c26cf3deb9fe31d78e2434b0