diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-12-25 16:23:50 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-12-25 16:23:50 +0100 |
commit | 251b2a1481a5927333f5d71a4f3e3232cd247575 (patch) | |
tree | b13c0850ff35ccdcc75eeab722b1ee36fcc9f8d8 | |
parent | 7adeb14d4596e4967273eda299c803e2a828ddbd (diff) |
cache manager retry refactor
-rw-r--r-- | oop2eindopdr/CacheManager.cpp | 50 | ||||
-rw-r--r-- | oop2eindopdr/CacheManager.h | 6 |
2 files changed, 27 insertions, 29 deletions
diff --git a/oop2eindopdr/CacheManager.cpp b/oop2eindopdr/CacheManager.cpp index 9324b81..6062b70 100644 --- a/oop2eindopdr/CacheManager.cpp +++ b/oop2eindopdr/CacheManager.cpp @@ -41,45 +41,37 @@ void CacheManager::update_cache() { date.close(); } +void CacheManager::retry(std::string label, std::function<void()> action, std::function<bool()> retry_if) { + for (unsigned int i = 0; i < max_tries; i++) { + action(); + if (retry_if() == false) return; + } + std::cout << "retry " << label << " failed after " << max_tries << " tries" << std::endl; +} + std::fstream* CacheManager::cache_get(std::string filename) { return cache_get(filename.c_str()); } std::fstream* CacheManager::cache_get(const char* filename) { std::filesystem::path fixed_path = prefix_cache_path(filename); // fix duplicate slashes and add prefix - unsigned int i = 0; - - i = 0; - while (!std::filesystem::exists(fixed_path.parent_path())) { + retry("mkdir", [&] { std::filesystem::create_directories(fixed_path.parent_path()); // make sure folder exists - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - i++; - if (i > 100) { - std::cout << "folder touch doesn't succeed after 100 tries on file " << fixed_path << std::endl; - exit(1); - } - } + }, [&] { + return !std::filesystem::exists(fixed_path.parent_path()); + }); - i = 0; - while (!std::filesystem::exists(fixed_path)) { + retry("touch file", [&] { std::fstream(fixed_path, std::ios::trunc | std::ios::out).close(); // touch file - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - i++; - if (i > 100) { - std::cout << "file touch doesn't succeed after 100 tries on file " << fixed_path << std::endl; - exit(1); - } - } + }, [&] { + return !std::filesystem::exists(fixed_path); + }); - i = 0; std::fstream* file = nullptr; - do { + retry("get file handle", [&] { delete file; file = new std::fstream(fixed_path.c_str(), std::ios::out | std::ios::in); // open file - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - i++; - if (i > 100) { - std::cout << "file open doesn't succeed after 100 tries on file " << fixed_path << std::endl; - exit(1); - } - } while (!file->is_open()); + }, [&] { + return !file->is_open(); + }); + this->files.push_back(file); // add file pointer (for closing all files on exit) return file; } diff --git a/oop2eindopdr/CacheManager.h b/oop2eindopdr/CacheManager.h index 0b15cfa..1e0a565 100644 --- a/oop2eindopdr/CacheManager.h +++ b/oop2eindopdr/CacheManager.h @@ -2,6 +2,8 @@ #include <string> #include <vector> +#include <chrono> +#include <functional> /** @brief cache storage manager */ class CacheManager { @@ -10,6 +12,10 @@ private: std::string cache_path; /** @brief pointers to open file handles */ std::vector<std::fstream*> files; + + const unsigned int max_tries = 100; + + virtual void retry(std::string label, std::function<void()> action, std::function<bool()> retry_if); public: /** @brief initialize CacheManager class at `cache_path` */ CacheManager(const char* cache_path); |