From c8d8dd7a88bae225bfa0e36ab6d80f7383887308 Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Thu, 21 Apr 2016 15:58:53 +0300 Subject: [downloader] Add auto retry download if internet connection blinked --- storage/storage.cpp | 33 +++++++++++++++++++++++++++++---- storage/storage.hpp | 7 +++++++ 2 files changed, 36 insertions(+), 4 deletions(-) (limited to 'storage') diff --git a/storage/storage.cpp b/storage/storage.cpp index 511a59e5a8..b4a8857072 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -22,6 +22,7 @@ #include "std/algorithm.hpp" #include "std/bind.hpp" +#include "std/chrono.hpp" #include "std/sstream.hpp" #include "std/target_os.hpp" @@ -108,8 +109,9 @@ MapFilesDownloader::TProgress Storage::GetOverallProgress(TCountriesVec const & } Storage::Storage(string const & pathToCountriesFile /* = COUNTRIES_FILE */, string const & dataDir /* = string() */) - : m_downloader(new HttpMapFilesDownloader()), m_currentSlotId(0), m_dataDir(dataDir), - m_downloadMapOnTheMap(nullptr) + : m_downloader(new HttpMapFilesDownloader()), m_currentSlotId(0), m_dataDir(dataDir) + , m_autoRetryWorker(seconds(20)) + , m_downloadMapOnTheMap(nullptr) { SetLocale(languages::GetCurrentTwine()); LoadCountriesFile(pathToCountriesFile, m_dataDir); @@ -117,8 +119,9 @@ Storage::Storage(string const & pathToCountriesFile /* = COUNTRIES_FILE */, stri Storage::Storage(string const & referenceCountriesTxtJsonForTesting, unique_ptr mapDownloaderForTesting) - : m_downloader(move(mapDownloaderForTesting)), m_currentSlotId(0), - m_downloadMapOnTheMap(nullptr) + : m_downloader(move(mapDownloaderForTesting)), m_currentSlotId(0) + , m_autoRetryWorker(seconds(20)) + , m_downloadMapOnTheMap(nullptr) { m_currentVersion = LoadCountries(referenceCountriesTxtJsonForTesting, m_countries, m_affiliations); @@ -546,7 +549,29 @@ void Storage::DownloadNextCountryFromQueue() ASSERT_THREAD_CHECKER(m_threadChecker, ()); if (m_queue.empty()) + { + if (!m_failedCountries.empty() && m_autoRetryCounter > 0) + { + auto needReload = m_failedCountries; + auto action = [this, needReload] + { + --m_autoRetryCounter; + for (auto const & country : needReload) + { + NodeStatuses status; + GetNodeStatuses(country, status); + if (status.m_error == NodeErrorCode::NoInetConnection) + RetryDownloadNode(country); + } + }; + m_autoRetryWorker.RestartWith([action]{Platform().RunOnGuiThread(action);}); + } + else + { + m_autoRetryCounter = kAutoRetryCounterMax; + } return; + } QueuedCountry & queuedCountry = m_queue.front(); TCountryId const & countryId = queuedCountry.GetCountryId(); diff --git a/storage/storage.hpp b/storage/storage.hpp index bbcf36bb42..eec72a493d 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -11,6 +11,7 @@ #include "platform/local_country_file.hpp" +#include "base/deferred_task.hpp" #include "base/thread_checker.hpp" #include "std/function.hpp" @@ -237,6 +238,12 @@ private: ThreadChecker m_threadChecker; + static size_t constexpr kAutoRetryCounterMax = 3; + size_t m_autoRetryCounter = kAutoRetryCounterMax; + my::DeferredTask m_autoRetryWorker; + + inline bool IsAutoRetryDownloadFailed() const { return m_autoRetryCounter == 0; } + void DownloadNextCountryFromQueue(); void LoadCountriesFile(string const & pathToCountriesFile, string const & dataDir, -- cgit v1.2.3