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/config/ConfigDefinition.hpp"
23#include "util/log/Logger.hpp"
24
25#include <boost/signals2/signal.hpp>
26
27#include <atomic>
28#include <chrono>
29#include <concepts>
30#include <condition_variable>
31#include <csignal>
32#include <cstdlib>
33#include <functional>
34#include <mutex>
35#include <thread>
36
37namespace util {
38namespace impl {
39
40class SignalsHandlerStatic;
41
42} // namespace impl
43
52 enum class State { WaitingForSignal, GracefulShutdown, ForceExit, NormalExit };
53
54 std::chrono::steady_clock::duration gracefulPeriod_;
55 std::function<void()> forceExitHandler_;
56
57 boost::signals2::signal<void()> stopSignal_;
58
59 std::atomic<bool> signalReceived_{false};
60 std::atomic<State> state_{State::WaitingForSignal};
61
62 std::mutex mutex_;
63 std::condition_variable cv_;
64 std::thread workerThread_;
65
66 friend class impl::SignalsHandlerStatic;
67
68public:
72 enum class Priority { StopFirst = 0, Normal = 1, StopLast = 2 };
73
82 std::function<void()> forceExitHandler = kDEFAULT_FORCE_EXIT_HANDLER
83 );
84
85 SignalsHandler(SignalsHandler const&) = delete;
88 operator=(SignalsHandler const&) = delete;
90 operator=(SignalsHandler&&) = delete;
91
96
104 template <std::invocable SomeCallback>
105 void
106 subscribeToStop(SomeCallback&& callback, Priority priority = Priority::Normal)
107 {
108 stopSignal_.connect(static_cast<int>(priority), std::forward<SomeCallback>(callback));
109 }
110
115 void
117
118 static constexpr auto kHANDLED_SIGNALS = {SIGINT, SIGTERM};
119
120private:
126 static void
127 setHandler(void (*handler)(int) = nullptr);
128
132 void
133 runStateMachine();
134
135 static constexpr auto kDEFAULT_FORCE_EXIT_HANDLER = []() { std::exit(EXIT_FAILURE); };
136};
137
138} // namespace util
Class handling signals.
Definition SignalsHandler.hpp:48
SignalsHandler(util::config::ClioConfigDefinition const &config, std::function< void()> forceExitHandler=kDEFAULT_FORCE_EXIT_HANDLER)
Create SignalsHandler object.
Definition SignalsHandler.cpp:68
Priority
Enum for stop priority.
Definition SignalsHandler.hpp:72
void subscribeToStop(SomeCallback &&callback, Priority priority=Priority::Normal)
Subscribe to stop signal.
Definition SignalsHandler.hpp:106
void notifyGracefulShutdownComplete()
Notify the signal handler that graceful shutdown has completed. This allows the handler to transition...
Definition SignalsHandler.cpp:91
~SignalsHandler()
Destructor of SignalsHandler.
Definition SignalsHandler.cpp:77
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
This namespace contains various utilities.
Definition AccountUtils.hpp:30