-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #442.
- Loading branch information
Showing
8 changed files
with
447 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of clio: https://github.com/XRPLF/clio | ||
Copyright (c) 2024, the clio developers. | ||
Permission to use, copy, modify, and distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#include "util/SignalsHandler.hpp" | ||
|
||
#include "util/Assert.hpp" | ||
#include "util/Constants.hpp" | ||
#include "util/config/Config.hpp" | ||
#include "util/log/Logger.hpp" | ||
|
||
#include <boost/asio/error.hpp> | ||
#include <boost/asio/executor_work_guard.hpp> | ||
|
||
#include <chrono> | ||
#include <cmath> | ||
#include <csignal> | ||
#include <cstddef> | ||
#include <functional> | ||
#include <optional> | ||
#include <string> | ||
#include <utility> | ||
|
||
namespace util { | ||
|
||
namespace impl { | ||
|
||
class SignalsHandlerStatic { | ||
static SignalsHandler* handler_; | ||
|
||
public: | ||
static void | ||
registerHandler(SignalsHandler& handler) | ||
{ | ||
ASSERT(handler_ == nullptr, "There could be only one instance of SignalsHandler"); | ||
handler_ = &handler; | ||
} | ||
|
||
static void | ||
resetHandler() | ||
{ | ||
handler_ = nullptr; | ||
} | ||
|
||
static void | ||
handleSignal(int signal) | ||
{ | ||
ASSERT(handler_ != nullptr, "SignalsHandler is not initialized"); | ||
handler_->stopHandler_(signal); | ||
} | ||
|
||
static void | ||
handleSecondSignal(int signal) | ||
{ | ||
ASSERT(handler_ != nullptr, "SignalsHandler is not initialized"); | ||
handler_->secondSignalHandler_(signal); | ||
} | ||
}; | ||
|
||
SignalsHandler* SignalsHandlerStatic::handler_ = nullptr; | ||
|
||
} // namespace impl | ||
|
||
SignalsHandler::SignalsHandler(Config const& config, std::function<void()> forceExitHandler) | ||
: gracefulPeriod_(0) | ||
, context_(1) | ||
, stopHandler_([this, forceExitHandler](int) mutable { | ||
LOG(LogService::info()) << "Got stop signal. Stopping Clio. Graceful period is " | ||
<< std::chrono::duration_cast<std::chrono::milliseconds>(gracefulPeriod_).count() | ||
<< " milliseconds."; | ||
setHandler(impl::SignalsHandlerStatic::handleSecondSignal); | ||
timer_.emplace(context_.scheduleAfter( | ||
gracefulPeriod_, | ||
[forceExitHandler = std::move(forceExitHandler)](auto&& stopToken, bool canceled) { | ||
// TODO: Update this after https://github.com/XRPLF/clio/issues/1367 | ||
if (not stopToken.isStopRequested() and not canceled) { | ||
LOG(LogService::warn()) << "Force exit at the end of graceful period."; | ||
forceExitHandler(); | ||
} | ||
} | ||
)); | ||
stopSignal_(); | ||
}) | ||
, secondSignalHandler_([this, forceExitHandler = std::move(forceExitHandler)](int) { | ||
LOG(LogService::warn()) << "Force exit on second signal."; | ||
forceExitHandler(); | ||
cancelTimer(); | ||
setHandler(); | ||
}) | ||
{ | ||
impl::SignalsHandlerStatic::registerHandler(*this); | ||
|
||
auto const gracefulPeriod = | ||
std::round(config.valueOr("graceful_period", 10.f) * static_cast<float>(util::MILLISECONDS_PER_SECOND)); | ||
ASSERT(gracefulPeriod >= 0.f, "Graceful period must be non-negative"); | ||
gracefulPeriod_ = std::chrono::milliseconds{static_cast<size_t>(gracefulPeriod)}; | ||
|
||
setHandler(impl::SignalsHandlerStatic::handleSignal); | ||
} | ||
|
||
SignalsHandler::~SignalsHandler() | ||
{ | ||
cancelTimer(); | ||
setHandler(); | ||
impl::SignalsHandlerStatic::resetHandler(); // This is needed mostly for tests to reset static state | ||
} | ||
|
||
void | ||
SignalsHandler::cancelTimer() | ||
{ | ||
if (timer_.has_value()) { | ||
timer_->cancel(); | ||
timer_->requestStop(); | ||
} | ||
} | ||
|
||
void | ||
SignalsHandler::setHandler(void (*handler)(int)) | ||
{ | ||
for (int const signal : HANDLED_SIGNALS) { | ||
std::signal(signal, handler == nullptr ? SIG_DFL : handler); | ||
} | ||
} | ||
|
||
} // namespace util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of clio: https://github.com/XRPLF/clio | ||
Copyright (c) 2024, the clio developers. | ||
Permission to use, copy, modify, and distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#pragma once | ||
|
||
#include "util/async/context/BasicExecutionContext.hpp" | ||
#include "util/config/Config.hpp" | ||
#include "util/log/Logger.hpp" | ||
|
||
#include <boost/asio/executor_work_guard.hpp> | ||
#include <boost/asio/io_context.hpp> | ||
#include <boost/asio/steady_timer.hpp> | ||
#include <boost/signals2/signal.hpp> | ||
#include <boost/signals2/variadic_signal.hpp> | ||
|
||
#include <chrono> | ||
#include <concepts> | ||
#include <csignal> | ||
#include <cstdlib> | ||
#include <functional> | ||
#include <optional> | ||
#include <string> | ||
|
||
namespace util { | ||
|
||
namespace impl { | ||
class SignalsHandlerStatic; | ||
} // namespace impl | ||
|
||
/** | ||
* @brief Class handling signals. | ||
* @note There could be only one instance of this class. | ||
*/ | ||
class SignalsHandler { | ||
std::chrono::steady_clock::duration gracefulPeriod_; | ||
async::PoolExecutionContext context_; | ||
std::optional<async::PoolExecutionContext::ScheduledOperation<void>> timer_; | ||
|
||
boost::signals2::signal<void()> stopSignal_; | ||
std::function<void(int)> stopHandler_; | ||
std::function<void(int)> secondSignalHandler_; | ||
|
||
friend class impl::SignalsHandlerStatic; | ||
|
||
public: | ||
/** | ||
* @brief Enum for stop priority. | ||
*/ | ||
enum class Priority { StopFirst = 0, Normal = 1, StopLast = 2 }; | ||
|
||
/** | ||
* @brief Create SignalsHandler object. | ||
* | ||
* @param config The configuration. | ||
* @param forceExitHandler The handler for forced exit. | ||
*/ | ||
SignalsHandler(Config const& config, std::function<void()> forceExitHandler = defaultForceExitHandler_); | ||
|
||
SignalsHandler(SignalsHandler const&) = delete; | ||
SignalsHandler(SignalsHandler&&) = delete; | ||
SignalsHandler& | ||
operator=(SignalsHandler const&) = delete; | ||
SignalsHandler& | ||
operator=(SignalsHandler&&) = delete; | ||
|
||
/** | ||
* @brief Destructor of SignalsHandler. | ||
*/ | ||
~SignalsHandler(); | ||
|
||
/** | ||
* @brief Subscribe to stop signal. | ||
* | ||
* @tparam SomeCallback The type of the callback. | ||
* @param callback The callback to call on stop signal. | ||
* @param priority The priority of the callback. Default is Normal. | ||
*/ | ||
template <std::invocable SomeCallback> | ||
void | ||
subscribeToStop(SomeCallback&& callback, Priority priority = Priority::Normal) | ||
{ | ||
stopSignal_.connect(static_cast<int>(priority), std::forward<SomeCallback>(callback)); | ||
} | ||
|
||
static constexpr auto HANDLED_SIGNALS = {SIGINT, SIGTERM}; | ||
|
||
private: | ||
/** | ||
* @brief Cancel scheduled force exit if any. | ||
*/ | ||
void | ||
cancelTimer(); | ||
|
||
/** | ||
* @brief Set signal handler for handled signals. | ||
* | ||
* @param handler The handler to set. Default is nullptr. | ||
*/ | ||
static void | ||
setHandler(void (*handler)(int) = nullptr); | ||
|
||
static auto constexpr defaultForceExitHandler_ = []() { std::exit(EXIT_FAILURE); }; | ||
}; | ||
|
||
} // namespace util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.