mirror of
https://github.com/XRPLF/clio.git
synced 2026-06-03 16:56:45 +00:00
feat: Graceful shutdown with old web server (#2786)
- Stop accepting connections during graceful shutdown in the old web server - Stop all the services before Clio exits - Move cache saving into stop callback
This commit is contained in:
70
src/data/LedgerCacheSaver.cpp
Normal file
70
src/data/LedgerCacheSaver.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2025, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "data/LedgerCacheSaver.hpp"
|
||||
|
||||
#include "data/LedgerCacheInterface.hpp"
|
||||
#include "util/Assert.hpp"
|
||||
#include "util/Profiler.hpp"
|
||||
#include "util/log/Logger.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace data {
|
||||
|
||||
LedgerCacheSaver::LedgerCacheSaver(util::config::ClioConfigDefinition const& config, LedgerCacheInterface const& cache)
|
||||
: cacheFilePath_(config.maybeValue<std::string>("cache.file.path")), cache_(cache)
|
||||
{
|
||||
}
|
||||
|
||||
LedgerCacheSaver::~LedgerCacheSaver()
|
||||
{
|
||||
waitToFinish();
|
||||
}
|
||||
|
||||
void
|
||||
LedgerCacheSaver::save()
|
||||
{
|
||||
ASSERT(not savingThread_.has_value(), "Multiple save() calls are not allowed");
|
||||
savingThread_ = std::thread([this]() {
|
||||
if (not cacheFilePath_.has_value()) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG(util::LogService::info()) << "Saving ledger cache to " << *cacheFilePath_;
|
||||
if (auto const [success, durationMs] = util::timed([&]() { return cache_.get().saveToFile(*cacheFilePath_); });
|
||||
success.has_value()) {
|
||||
LOG(util::LogService::info()) << "Successfully saved ledger cache in " << durationMs << " ms";
|
||||
} else {
|
||||
LOG(util::LogService::error()) << "Error saving LedgerCache to file";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
LedgerCacheSaver::waitToFinish()
|
||||
{
|
||||
if (savingThread_.has_value() and savingThread_->joinable()) {
|
||||
savingThread_->join();
|
||||
}
|
||||
savingThread_.reset();
|
||||
}
|
||||
|
||||
} // namespace data
|
||||
Reference in New Issue
Block a user