|
| 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 | + |
| 37 | +namespace app { |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief Application stopper class. On stop it will create a new thread to run all the shutdown tasks. |
| 41 | + */ |
| 42 | +class Stopper { |
| 43 | + boost::asio::io_context ctx_; |
| 44 | + std::thread worker_; |
| 45 | + |
| 46 | +public: |
| 47 | + /** |
| 48 | + * @brief Destroy the Stopper object |
| 49 | + */ |
| 50 | + ~Stopper(); |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief Set the callback to be called when the application is stopped. |
| 54 | + * |
| 55 | + * @param cb The callback to be called on application stop. |
| 56 | + */ |
| 57 | + void |
| 58 | + setOnStop(std::function<void(boost::asio::yield_context)> cb); |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Stop the application and run the shutdown tasks. |
| 62 | + */ |
| 63 | + void |
| 64 | + stop(); |
| 65 | + |
| 66 | + /** |
| 67 | + * @brief Create a callback to be called on application stop. |
| 68 | + * |
| 69 | + * @param server The server to stop. |
| 70 | + * @param balancer The load balancer to stop. |
| 71 | + * @param etl The ETL service to stop. |
| 72 | + * @param subscriptions The subscription manager to stop. |
| 73 | + * @param backend The backend to stop. |
| 74 | + * @param ioc The io_context to stop. |
| 75 | + * @return The callback to be called on application stop. |
| 76 | + */ |
| 77 | + template < |
| 78 | + web::ng::SomeServer ServerType, |
| 79 | + etl::SomeLoadBalancer LoadBalancerType, |
| 80 | + etl::SomeETLService ETLServiceType> |
| 81 | + static std::function<void(boost::asio::yield_context)> |
| 82 | + makeOnStopCallback( |
| 83 | + ServerType& server, |
| 84 | + LoadBalancerType& balancer, |
| 85 | + ETLServiceType& etl, |
| 86 | + feed::SubscriptionManagerInterface& subscriptions, |
| 87 | + data::BackendInterface& backend, |
| 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 |
0 commit comments