aboutsummaryrefslogtreecommitdiff
path: root/oop2eindopdr/CacheManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'oop2eindopdr/CacheManager.cpp')
-rw-r--r--oop2eindopdr/CacheManager.cpp50
1 files changed, 21 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;
}