Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SignalsHandler.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/async/context/BasicExecutionContext.hpp"
23#include "util/config/ConfigDefinition.hpp"
24#include "util/log/Logger.hpp"
25
26#include <boost/asio/executor_work_guard.hpp>
27#include <boost/asio/io_context.hpp>
28#include <boost/asio/steady_timer.hpp>
29#include <boost/signals2/signal.hpp>
30#include <boost/signals2/variadic_signal.hpp>
31
32#include <chrono>
33#include <concepts>
34#include <csignal>
35#include <cstdlib>
36#include <functional>
37#include <optional>
38
39namespace util {
40namespace impl {
41
42class SignalsHandlerStatic;
43
44} // namespace impl
45
51 std::chrono::steady_clock::duration gracefulPeriod_;
53 std::optional<async::PoolExecutionContext::ScheduledOperation<void>> timer_;
54
55 boost::signals2::signal<void()> stopSignal_;
56 std::function<void(int)> stopHandler_;
57 std::function<void(int)> secondSignalHandler_;
58
59 friend class impl::SignalsHandlerStatic;
60
61public:
65 enum class Priority { StopFirst = 0, Normal = 1, StopLast = 2 };
66
75 std::function<void()> forceExitHandler = kDEFAULT_FORCE_EXIT_HANDLER
76 );
77
78 SignalsHandler(SignalsHandler const&) = delete;
81 operator=(SignalsHandler const&) = delete;
83 operator=(SignalsHandler&&) = delete;
84
89
97 template <std::invocable SomeCallback>
98 void
99 subscribeToStop(SomeCallback&& callback, Priority priority = Priority::Normal)
100 {
101 stopSignal_.connect(static_cast<int>(priority), std::forward<SomeCallback>(callback));
102 }
103
104 static constexpr auto kHANDLED_SIGNALS = {SIGINT, SIGTERM};
105
106private:
110 void
111 cancelTimer();
112
118 static void
119 setHandler(void (*handler)(int) = nullptr);
120
121 static constexpr auto kDEFAULT_FORCE_EXIT_HANDLER = []() { std::exit(EXIT_FAILURE); };
122};
123
124} // namespace util
Class handling signals.
Definition SignalsHandler.hpp:50
SignalsHandler(util::config::ClioConfigDefinition const &config, std::function< void()> forceExitHandler=kDEFAULT_FORCE_EXIT_HANDLER)
Create SignalsHandler object.
Definition SignalsHandler.cpp:71
Priority
Enum for stop priority.
Definition SignalsHandler.hpp:65
void subscribeToStop(SomeCallback &&callback, Priority priority=Priority::Normal)
Subscribe to stop signal.
Definition SignalsHandler.hpp:99
~SignalsHandler()
Destructor of SignalsHandler.
Definition SignalsHandler.cpp:103
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
BasicExecutionContext< impl::AsioPoolContext, impl::BasicStopSource, impl::PostDispatchStrategy > PoolExecutionContext
A asio::thread_pool-based execution context.
Definition BasicExecutionContext.hpp:414
This namespace contains various utilities.
Definition AccountUtils.hpp:30