Skip to content

Commit

Permalink
Merge pull request #280 from dpaulat/hotfix/refresh-failed-placefiles
Browse files Browse the repository at this point in the history
Automatically Refresh Failed Placfiles
  • Loading branch information
dpaulat authored Oct 6, 2024
2 parents d92f6c7 + 621cbb3 commit 411c570
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
44 changes: 40 additions & 4 deletions scwx-qt/source/scwx/qt/manager/placefile_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class PlacefileManager::Impl::PlacefileRecord

void CancelRefresh();
void ScheduleRefresh();
void ScheduleRefresh(
const std::chrono::system_clock::duration timeUntilNextUpdate);
void Update();
void UpdateAsync();

Expand Down Expand Up @@ -150,6 +152,8 @@ class PlacefileManager::Impl::PlacefileRecord

std::string lastRadarSite_ {};
std::chrono::system_clock::time_point lastUpdateTime_ {};

std::size_t failureCount_ {};
};

PlacefileManager::PlacefileManager() : p(std::make_unique<Impl>(this))
Expand Down Expand Up @@ -344,8 +348,8 @@ PlacefileManager::Impl::PlacefileRecord::refresh_time() const

if (refresh_enabled())
{
// Don't refresh more often than every 15 seconds
return std::max(placefile_->refresh(), 15s);
// Don't refresh more often than every 1 second
return std::max(placefile_->refresh(), 1s);
}

return -1s;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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();
}
else if (enabled_)
{
using namespace std::chrono_literals;

// Update refresh timer
ScheduleRefresh();
++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()
Expand All @@ -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),
Expand Down
10 changes: 9 additions & 1 deletion wxdata/source/scwx/gr/placefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 411c570

Please sign in to comment.