From 0f03efe868f4a7910678a0a729f437a2fe8986b7 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 5 Feb 2024 11:27:09 +1100 Subject: [PATCH] Make db updates more robust again the sd card disappearing Accidentally found a bug while live on youtube :) --- src/database/database.cpp | 23 +++++++++++++++++------ src/database/file_gatherer.cpp | 3 ++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/database/database.cpp b/src/database/database.cpp index 141482ed..15e7060d 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -274,12 +274,27 @@ auto Database::getIndexes() -> std::vector { }; } +class UpdateNotifier { + public: + UpdateNotifier(std::atomic& is_updating) : is_updating_(is_updating) { + events::Ui().Dispatch(event::UpdateStarted{}); + events::System().Dispatch(event::UpdateStarted{}); + } + ~UpdateNotifier() { + is_updating_ = false; + events::Ui().Dispatch(event::UpdateFinished{}); + events::System().Dispatch(event::UpdateFinished{}); + } + + private: + std::atomic& is_updating_; +}; + auto Database::updateIndexes() -> void { if (is_updating_.exchange(true)) { return; } - events::Ui().Dispatch(event::UpdateStarted{}); - events::System().Dispatch(event::UpdateStarted{}); + UpdateNotifier notifier{is_updating_}; leveldb::ReadOptions read_options; read_options.fill_cache = false; @@ -453,10 +468,6 @@ auto Database::updateIndexes() -> void { dbSetLastUpdate(newest_track); ESP_LOGI(kTag, "newest track was at %u,%u", newest_track.first, newest_track.second); - - is_updating_ = false; - events::Ui().Dispatch(event::UpdateFinished{}); - events::System().Dispatch(event::UpdateFinished{}); } auto Database::isUpdating() -> bool { diff --git a/src/database/file_gatherer.cpp b/src/database/file_gatherer.cpp index f07a1b4d..dde363bd 100644 --- a/src/database/file_gatherer.cpp +++ b/src/database/file_gatherer.cpp @@ -28,6 +28,8 @@ auto FileGathererImpl::FindFiles( while (!to_explore.empty()) { std::string next_path_str = to_explore.front(); + to_explore.pop_front(); + const TCHAR* next_path = static_cast(next_path_str.c_str()); FF_DIR dir; @@ -72,7 +74,6 @@ auto FileGathererImpl::FindFiles( auto lock = drivers::acquire_spi(); f_closedir(&dir); - to_explore.pop_front(); } }