2525#include " data/AmendmentCenter.hpp"
2626#include " data/BackendFactory.hpp"
2727#include " data/LedgerCache.hpp"
28+ #include " data/LedgerCacheSaver.hpp"
2829#include " etl/ETLService.hpp"
2930#include " etl/LoadBalancer.hpp"
3031#include " etl/NetworkValidatedLedgers.hpp"
@@ -98,36 +99,23 @@ ClioApplication::run(bool const useNgWebServer)
9899 auto const threads = config_.get <uint16_t >(" io_threads" );
99100 LOG (util::LogService::info ()) << " Number of io threads = " << threads;
100101
101- // IO context to handle all incoming requests, as well as other things.
102- // This is not the only io context in the application.
103- boost::asio::io_context ioc{threads};
104-
105102 // Similarly we need a context to run ETL on
106103 // In the future we can remove the raw ioc and use ctx instead
104+ // This context should be above ioc because its reference is getting into tasks inside ioc
107105 util::async::CoroExecutionContext ctx{threads};
108106
107+ // IO context to handle all incoming requests, as well as other things.
108+ // This is not the only io context in the application.
109+ boost::asio::io_context ioc{threads};
110+
109111 // Rate limiter, to prevent abuse
110112 auto whitelistHandler = web::dosguard::WhitelistHandler{config_};
111113 auto const dosguardWeights = web::dosguard::Weights::make (config_);
112114 auto dosGuard = web::dosguard::DOSGuard{config_, whitelistHandler, dosguardWeights};
113115 auto sweepHandler = web::dosguard::IntervalSweepHandler{config_, ioc, dosGuard};
114116
115117 auto cache = data::LedgerCache{};
116- appStopper_.setOnStop ([&cache, this ](auto &&) {
117- // TODO(kuznetsss): move this into Stopper::makeOnStopCallback()
118- auto const cacheFilePath = config_.maybeValue <std::string>(" cache.file.path" );
119- if (not cacheFilePath.has_value ()) {
120- return ;
121- }
122-
123- LOG (util::LogService::info ()) << " Saving ledger cache to " << *cacheFilePath;
124- if (auto const [success, duration_ms] = util::timed ([&]() { return cache.saveToFile (*cacheFilePath); });
125- success.has_value ()) {
126- LOG (util::LogService::info ()) << " Successfully saved ledger cache in " << duration_ms << " ms" ;
127- } else {
128- LOG (util::LogService::error ()) << " Error saving LedgerCache to file" ;
129- }
130- });
118+ auto cacheSaver = data::LedgerCacheSaver{config_, cache};
131119
132120 // Interface to the database
133121 auto backend = data::makeBackend (config_, cache);
@@ -208,7 +196,7 @@ ClioApplication::run(bool const useNgWebServer)
208196 }
209197
210198 appStopper_.setOnStop (
211- Stopper::makeOnStopCallback (httpServer.value (), *balancer, *etl, *subscriptions, *backend, ioc)
199+ Stopper::makeOnStopCallback (httpServer.value (), *balancer, *etl, *subscriptions, *backend, cacheSaver, ioc)
212200 );
213201
214202 // Blocks until stopped.
@@ -223,6 +211,9 @@ ClioApplication::run(bool const useNgWebServer)
223211 auto handler = std::make_shared<web::RPCServerHandler<RPCEngineType>>(config_, backend, rpcEngine, etl, dosGuard);
224212
225213 auto const httpServer = web::makeHttpServer (config_, ioc, dosGuard, handler, cache);
214+ appStopper_.setOnStop (
215+ Stopper::makeOnStopCallback (*httpServer, *balancer, *etl, *subscriptions, *backend, cacheSaver, ioc)
216+ );
226217
227218 // Blocks until stopped.
228219 // When stopped, shared_ptrs fall out of scope
0 commit comments