Automatically refresh placefiles that failed to load

This commit is contained in:
Dan Paulat 2024-10-04 05:40:00 -05:00
parent d92f6c7204
commit 06a2a18c06
2 changed files with 48 additions and 4 deletions

View file

@ -105,6 +105,8 @@ public:
void CancelRefresh();
void ScheduleRefresh();
void ScheduleRefresh(
const std::chrono::system_clock::duration timeUntilNextUpdate);
void Update();
void UpdateAsync();
@ -150,6 +152,8 @@ public:
std::string lastRadarSite_ {};
std::chrono::system_clock::time_point lastUpdateTime_ {};
std::size_t failureCount_ {};
};
PlacefileManager::PlacefileManager() : p(std::make_unique<Impl>(this))
@ -542,6 +546,11 @@ void PlacefileManager::Impl::PlacefileRecord::Update()
if (url.isLocalFile())
{
updatedPlacefile = gr::Placefile::Load(name);
if (updatedPlacefile == nullptr)
{
logger_->error("Local placefile not found: {}", name);
}
}
else
{
@ -625,6 +634,7 @@ void PlacefileManager::Impl::PlacefileRecord::Update()
placefile_ = updatedPlacefile;
title_ = placefile_->title();
lastUpdateTime_ = std::chrono::system_clock::now();
failureCount_ = 0;
// Update font resources
{
@ -645,10 +655,30 @@ void PlacefileManager::Impl::PlacefileRecord::Update()
// Notify slots of the placefile update
Q_EMIT p->self_->PlacefileUpdated(name);
}
}
// Update refresh timer
ScheduleRefresh();
// Update refresh timer
ScheduleRefresh();
}
else if (enabled_)
{
using namespace std::chrono_literals;
++failureCount_;
// Update refresh timer if the file failed to load, in case it is able to
// be resolved later
if (url.isLocalFile())
{
ScheduleRefresh(10s);
}
else
{
// Start attempting to refresh at 15 seconds, and start backing off
// until retrying every 60 seconds
ScheduleRefresh(
std::min<std::chrono::seconds>(15s * failureCount_, 60s));
}
}
}
void PlacefileManager::Impl::PlacefileRecord::ScheduleRefresh()
@ -666,6 +696,12 @@ void PlacefileManager::Impl::PlacefileRecord::ScheduleRefresh()
auto nextUpdateTime = lastUpdateTime_ + refresh_time();
auto timeUntilNextUpdate = nextUpdateTime - std::chrono::system_clock::now();
ScheduleRefresh(timeUntilNextUpdate);
}
void PlacefileManager::Impl::PlacefileRecord::ScheduleRefresh(
const std::chrono::system_clock::duration timeUntilNextUpdate)
{
logger_->debug(
"Scheduled refresh in {:%M:%S} ({})",
std::chrono::duration_cast<std::chrono::seconds>(timeUntilNextUpdate),

View file

@ -154,9 +154,17 @@ std::shared_ptr<Placefile::Font> Placefile::font(std::size_t i)
std::shared_ptr<Placefile> Placefile::Load(const std::string& filename)
{
std::shared_ptr<Placefile> placefile = nullptr;
logger_->debug("Loading placefile: {}", filename);
std::ifstream f(filename, std::ios_base::in);
return Load(filename, f);
if (f.is_open())
{
placefile = Load(filename, f);
}
return placefile;
}
std::shared_ptr<Placefile> Placefile::Load(const std::string& name,