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 "etlng/ETLServiceInterface.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 <web::ng::SomeServer ServerType>
78 static std::function<void(boost::asio::yield_context)>
80 ServerType& server,
85 boost::asio::io_context& ioc
86 )
87 {
88 return [&](boost::asio::yield_context yield) {
89 util::CoroutineGroup coroutineGroup{yield};
90 coroutineGroup.spawn(yield, [&server](auto innerYield) {
91 server.stop(innerYield);
92 LOG(util::LogService::info()) << "Server stopped";
93 });
94 coroutineGroup.spawn(yield, [&balancer](auto innerYield) {
95 balancer.stop(innerYield);
96 LOG(util::LogService::info()) << "LoadBalancer stopped";
97 });
98 coroutineGroup.asyncWait(yield);
99
100 etl.stop();
101 LOG(util::LogService::info()) << "ETL stopped";
102
103 subscriptions.stop();
104 LOG(util::LogService::info()) << "SubscriptionManager stopped";
105
106 backend.waitForWritesToFinish();
107 LOG(util::LogService::info()) << "Backend writes finished";
108
109 ioc.stop();
110 LOG(util::LogService::info()) << "io_context stopped";
111 };
112 }
113};
114
115} // namespace app
Application stopper class. On stop it will create a new thread to run all the shutdown tasks.
Definition Stopper.hpp:42
static std::function< void(boost::asio::yield_context)> makeOnStopCallback(ServerType &server, etlng::LoadBalancerInterface &balancer, etlng::ETLServiceInterface &etl, feed::SubscriptionManagerInterface &subscriptions, data::BackendInterface &backend, boost::asio::io_context &ioc)
Create a callback to be called on application stop.
Definition Stopper.hpp:79
~Stopper()
Destroy the Stopper object.
Definition Stopper.cpp:30
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:140
virtual void waitForWritesToFinish()=0
Wait for all pending writes to finish.
An interface for LoadBalancer.
Definition LoadBalancerInterface.hpp:45
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: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 accessible General logger at Severity::NFO severity.
Definition Logger.hpp:318
This namespace contains everything to do with the ETL and ETL sources.
Definition CacheLoader.hpp:39
This is a base class for any ETL service implementations.
Definition ETLServiceInterface.hpp:36