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/log/Logger.hpp"
24#include "util/newconfig/ConfigDefinition.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 {
40
41namespace impl {
42class SignalsHandlerStatic;
43} // namespace impl
44
50 std::chrono::steady_clock::duration gracefulPeriod_;
52 std::optional<async::PoolExecutionContext::ScheduledOperation<void>> timer_;
53
54 boost::signals2::signal<void()> stopSignal_;
55 std::function<void(int)> stopHandler_;
56 std::function<void(int)> secondSignalHandler_;
57
58 friend class impl::SignalsHandlerStatic;
59
60public:
64 enum class Priority { StopFirst = 0, Normal = 1, StopLast = 2 };
65
74 std::function<void()> forceExitHandler = kDEFAULT_FORCE_EXIT_HANDLER
75 );
76
77 SignalsHandler(SignalsHandler const&) = delete;
80 operator=(SignalsHandler const&) = delete;
82 operator=(SignalsHandler&&) = delete;
83
88
96 template <std::invocable SomeCallback>
97 void
98 subscribeToStop(SomeCallback&& callback, Priority priority = Priority::Normal)
99 {
100 stopSignal_.connect(static_cast<int>(priority), std::forward<SomeCallback>(callback));
101 }
102
103 static constexpr auto kHANDLED_SIGNALS = {SIGINT, SIGTERM};
104
105private:
109 void
110 cancelTimer();
111
117 static void
118 setHandler(void (*handler)(int) = nullptr);
119
120 static constexpr auto kDEFAULT_FORCE_EXIT_HANDLER = []() { std::exit(EXIT_FAILURE); };
121};
122
123} // namespace util
Class handling signals.
Definition SignalsHandler.hpp:49
SignalsHandler(util::config::ClioConfigDefinition const &config, std::function< void()> forceExitHandler=kDEFAULT_FORCE_EXIT_HANDLER)
Create SignalsHandler object.
Definition SignalsHandler.cpp:72
Priority
Enum for stop priority.
Definition SignalsHandler.hpp:64
void subscribeToStop(SomeCallback &&callback, Priority priority=Priority::Normal)
Subscribe to stop signal.
Definition SignalsHandler.hpp:98
~SignalsHandler()
Destructor of SignalsHandler.
Definition SignalsHandler.cpp:105
A highly configurable execution context.
Definition BasicExecutionContext.hpp:132
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
This namespace contains various utilities.
Definition AccountUtils.hpp:30