feat(ServerApplication): termination callback #4643 (#4733)

* feat(ServerApplication): termination callback #4643

* chore(ServerApplication): spelling fix #4643

* chore(ServerApplication): correct documentation #4643
This commit is contained in:
Aleksandar Fabijanic 2024-10-15 10:49:28 -05:00 committed by GitHub
parent 71a085c1dc
commit f0a29487b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
#include "Poco/NamedEvent.h" #include "Poco/NamedEvent.h"
#endif #endif
#include <functional>
namespace Poco { namespace Poco {
@ -122,8 +123,20 @@ class Util_API ServerApplication: public Application
/// --pidfile=/var/run/sample.pid) may be useful to record the process ID of /// --pidfile=/var/run/sample.pid) may be useful to record the process ID of
/// the daemon in a file. The PID file will be removed when the daemon process /// the daemon in a file. The PID file will be removed when the daemon process
/// terminates (but not, if it crashes). /// terminates (but not, if it crashes).
///
/// An application can register a callback to be called at termination time.
/// An example of the termination callback registration at some point
/// during the ServerApplication initialization time:
///
/// auto tCB = [](const std::string& message)
/// {
/// std::cout << message << std::endl;
/// };
/// ServerApplication::registerTerminateCallback(tCB, "custom termination message"s);
{ {
public: public:
using TerminateCallback = std::function<void(const std::string&)>;
ServerApplication(); ServerApplication();
/// Creates the ServerApplication. /// Creates the ServerApplication.
@ -158,6 +171,12 @@ public:
/// waitForTerminationRequest(), this method will return /// waitForTerminationRequest(), this method will return
/// and the application can shut down. /// and the application can shut down.
static void registerTerminateCallback(TerminateCallback tCB,
const std::string& message = _terminateMessage);
/// Registers a termination callback.
/// Used to register a function to be executed when the system
/// shutdown starts.
protected: protected:
int run(); int run();
void waitForTerminationRequest(); void waitForTerminationRequest();
@ -207,6 +226,11 @@ private:
static SERVICE_STATUS_HANDLE _serviceStatusHandle; static SERVICE_STATUS_HANDLE _serviceStatusHandle;
static Poco::NamedEvent _terminate; static Poco::NamedEvent _terminate;
#endif #endif
static void terminateCallback();
inline static std::atomic<bool> _terminationGuard = false;
inline static TerminateCallback _terminateCallback = nullptr;
inline static std::string _terminateMessage = "System terminating now!";
}; };

View File

@ -91,8 +91,22 @@ int ServerApplication::run()
} }
void ServerApplication::terminateCallback()
{
if (!_terminationGuard.exchange(true))
{
if (_terminateCallback)
{
_terminateCallback(_terminateMessage);
_terminateCallback = nullptr;
}
}
}
void ServerApplication::terminate() void ServerApplication::terminate()
{ {
terminateCallback();
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
_terminate.set(); _terminate.set();
#elif defined(POCO_VXWORKS) || POCO_OS == POCO_OS_ANDROID #elif defined(POCO_VXWORKS) || POCO_OS == POCO_OS_ANDROID
@ -103,6 +117,13 @@ void ServerApplication::terminate()
} }
void ServerApplication::registerTerminateCallback(TerminateCallback tCB, const std::string& message)
{
_terminateCallback = tCB;
_terminateMessage = message;
}
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
@ -196,6 +217,7 @@ void ServerApplication::waitForTerminationRequest()
{ {
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
_terminate.wait(); _terminate.wait();
terminateCallback();
_terminated.set(); _terminated.set();
} }
@ -444,6 +466,7 @@ void ServerApplication::handleStartup(const std::string& name, const std::string
void ServerApplication::waitForTerminationRequest() void ServerApplication::waitForTerminationRequest()
{ {
_terminate.wait(); _terminate.wait();
terminateCallback();
} }
@ -503,8 +526,10 @@ void ServerApplication::waitForTerminationRequest()
sigprocmask(SIG_BLOCK, &sset, NULL); sigprocmask(SIG_BLOCK, &sset, NULL);
int sig; int sig;
sigwait(&sset, &sig); sigwait(&sset, &sig);
terminateCallback();
#else // POCO_OS != POCO_OS_ANDROID #else // POCO_OS != POCO_OS_ANDROID
_terminate.wait(); _terminate.wait();
terminateCallback();
#endif #endif
} }