Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Stopper.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 "data/BackendInterface.hpp"
23#include "etl/ETLService.hpp"
24#include "etl/LoadBalancer.hpp"
25#include "feed/SubscriptionManagerInterface.hpp"
26#include "util/CoroutineGroup.hpp"
27#include "util/log/Logger.hpp"
28#include "web/ng/Server.hpp"
29
30#include <boost/asio/executor_work_guard.hpp>
31#include <boost/asio/io_context.hpp>
32#include <boost/asio/spawn.hpp>
33
34#include <functional>
35#include <thread>
36
37namespace app {
38
42class Stopper {
43 boost::asio::io_context ctx_;
44 std::thread worker_;
45
46public:
50 ~Stopper();
51
57 void
58 setOnStop(std::function<void(boost::asio::yield_context)> cb);
59
63 void
64 stop();
65
77 template <
78 web::ng::SomeServer ServerType,
79 etl::SomeLoadBalancer LoadBalancerType,
80 etl::SomeETLService ETLServiceType>
81 static std::function<void(boost::asio::yield_context)>
83 ServerType& server,
84 LoadBalancerType& balancer,
85 ETLServiceType& etl,
88 boost::asio::io_context& ioc
89 )
90 {
91 return [&](boost::asio::yield_context yield) {
92 util::CoroutineGroup coroutineGroup{yield};
93 coroutineGroup.spawn(yield, [&server](auto innerYield) {
94 server.stop(innerYield);
95 LOG(util::LogService::info()) << "Server stopped";
96 });
97 coroutineGroup.spawn(yield, [&balancer](auto innerYield) {
98 balancer.stop(innerYield);
99 LOG(util::LogService::info()) << "LoadBalancer stopped";
100 });
101 coroutineGroup.asyncWait(yield);
102
103 etl.stop();
104 LOG(util::LogService::info()) << "ETL stopped";
105
106 subscriptions.stop();
107 LOG(util::LogService::info()) << "SubscriptionManager stopped";
108
109 backend.waitForWritesToFinish();
110 LOG(util::LogService::info()) << "Backend writes finished";
111
112 ioc.stop();
113 LOG(util::LogService::info()) << "io_context stopped";
114 };
115 }
116};
117
118} // namespace app
Application stopper class. On stop it will create a new thread to run all the shutdown tasks.
Definition Stopper.hpp:42
~Stopper()
Destroy the Stopper object.
Definition Stopper.cpp:30
static std::function< void(boost::asio::yield_context)> makeOnStopCallback(ServerType &server, LoadBalancerType &balancer, ETLServiceType &etl, feed::SubscriptionManagerInterface &subscriptions, data::BackendInterface &backend, boost::asio::io_context &ioc)
Create a callback to be called on application stop.
Definition Stopper.hpp:82
void setOnStop(std::function< void(boost::asio::yield_context)> cb)
Set the callback to be called when the application is stopped.
Definition Stopper.cpp:37
void stop()
Stop the application and run the shutdown tasks.
Definition Stopper.cpp:43
The interface to the database used by Clio.
Definition BackendInterface.hpp:138
virtual void waitForWritesToFinish()=0
Wait for all pending writes to finish.
Interface of subscription manager. A subscription manager is responsible for managing the subscriptio...
Definition SubscriptionManagerInterface.hpp:44
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:37
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:45
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::NFO severity.
Definition Logger.hpp:316
Definition ETLService.hpp:70
Definition LoadBalancer.hpp:63
Definition Server.hpp:52
This namespace contains everything to do with the ETL and ETL sources.
Definition CacheLoader.hpp:36