Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Stopper.hpp
1#pragma once
2
3#include "cluster/Concepts.hpp"
4#include "data/BackendInterface.hpp"
5#include "data/LedgerCacheSaver.hpp"
6#include "etl/ETLServiceInterface.hpp"
8#include "feed/SubscriptionManagerInterface.hpp"
9#include "util/CoroutineGroup.hpp"
10#include "util/log/Logger.hpp"
11#include "web/interface/Concepts.hpp"
12
13#include <boost/asio/executor_work_guard.hpp>
14#include <boost/asio/io_context.hpp>
15#include <boost/asio/spawn.hpp>
16
17#include <functional>
18#include <thread>
19
20namespace app {
21
26class Stopper {
27 boost::asio::io_context ctx_;
28 std::thread worker_;
29 std::function<void()> onCompleteCallback_;
30
31public:
35 ~Stopper();
36
42 void
43 setOnStop(std::function<void(boost::asio::yield_context)> cb);
44
50 void
51 setOnComplete(std::function<void()> cb);
52
56 void
57 stop();
58
72 template <
73 web::SomeServer ServerType,
74 data::SomeLedgerCacheSaver LedgerCacheSaverType,
75 cluster::SomeClusterCommunicationService ClusterCommunicationServiceType>
76 static std::function<void(boost::asio::yield_context)>
78 ServerType& server,
83 LedgerCacheSaverType& cacheSaver,
84 ClusterCommunicationServiceType& clusterCommunicationService,
85 boost::asio::io_context& ioc
86 )
87 {
88 return [&](boost::asio::yield_context yield) {
89 cacheSaver.save();
90
91 util::CoroutineGroup coroutineGroup{yield};
92 coroutineGroup.spawn(yield, [&server](auto innerYield) {
93 server.stop(innerYield);
94 LOG(util::LogService::info()) << "Server stopped";
95 });
96 coroutineGroup.spawn(yield, [&balancer](auto innerYield) {
97 balancer.stop(innerYield);
98 LOG(util::LogService::info()) << "LoadBalancer stopped";
99 });
100 coroutineGroup.asyncWait(yield);
101
102 clusterCommunicationService.stop();
103
104 etl.stop();
105 LOG(util::LogService::info()) << "ETL stopped";
106
107 subscriptions.stop();
108 LOG(util::LogService::info()) << "SubscriptionManager stopped";
109
110 backend.waitForWritesToFinish();
111 LOG(util::LogService::info()) << "Backend writes finished";
112
113 cacheSaver.waitToFinish();
114
115 ioc.stop();
116 LOG(util::LogService::info()) << "io_context stopped";
117
119 };
120 }
121};
122
123} // namespace app
Application stopper class. On stop it will create a new thread to run all the shutdown tasks.
Definition Stopper.hpp:26
void setOnComplete(std::function< void()> cb)
Set the callback to be called when graceful shutdown completes.
Definition Stopper.cpp:31
~Stopper()
Destroy the Stopper object.
Definition Stopper.cpp:13
void setOnStop(std::function< void(boost::asio::yield_context)> cb)
Set the callback to be called when the application is stopped.
Definition Stopper.cpp:20
void stop()
Stop the application and run the shutdown tasks.
Definition Stopper.cpp:37
static std::function< void(boost::asio::yield_context)> makeOnStopCallback(ServerType &server, etl::LoadBalancerInterface &balancer, etl::ETLServiceInterface &etl, feed::SubscriptionManagerInterface &subscriptions, data::BackendInterface &backend, LedgerCacheSaverType &cacheSaver, ClusterCommunicationServiceType &clusterCommunicationService, boost::asio::io_context &ioc)
Create a callback to be called on application stop.
Definition Stopper.hpp:77
The interface to the database used by Clio.
Definition BackendInterface.hpp:125
virtual void waitForWritesToFinish()=0
Wait for all pending writes to finish.
An interface for LoadBalancer.
Definition LoadBalancerInterface.hpp:40
virtual void stop(boost::asio::yield_context yield)=0
Stop the load balancer. This will stop all subscription sources.
Interface of subscription manager. A subscription manager is responsible for managing the subscriptio...
Definition SubscriptionManagerInterface.hpp:25
virtual void stop()=0
Stop the SubscriptionManager and wait for all jobs to finish.
CoroutineGroup is a helper class to manage a group of coroutines. It allows to spawn multiple corouti...
Definition CoroutineGroup.hpp:18
void asyncWait(boost::asio::yield_context yield)
Wait for all the coroutines in the group to finish.
Definition CoroutineGroup.cpp:60
bool spawn(boost::asio::yield_context yield, std::function< void(boost::asio::yield_context)> fn)
Spawn a new coroutine in the group.
Definition CoroutineGroup.cpp:31
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:384
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:406
A concept for a class that can save ledger cache asynchronously.
Definition LedgerCacheSaver.hpp:21
Definition Concepts.hpp:36
This is a base class for any ETL service implementations.
Definition ETLServiceInterface.hpp:17