diff options
Diffstat (limited to 'oop2eindopdr/CacheManager.cpp')
| -rw-r--r-- | oop2eindopdr/CacheManager.cpp | 16 | 
1 files changed, 8 insertions, 8 deletions
diff --git a/oop2eindopdr/CacheManager.cpp b/oop2eindopdr/CacheManager.cpp index 6062b70..c335de3 100644 --- a/oop2eindopdr/CacheManager.cpp +++ b/oop2eindopdr/CacheManager.cpp @@ -41,31 +41,32 @@ void CacheManager::update_cache() {  	date.close();  } -void CacheManager::retry(std::string label, std::function<void()> action, std::function<bool()> retry_if) { +void CacheManager::retry(std::string label, bool check_pre, std::function<void()> action, std::function<bool()> retry_if) {  	for (unsigned int i = 0; i < max_tries; i++) { +		if (check_pre && retry_if() == false) return;  		action(); -		if (retry_if() == false) return; +		std::this_thread::sleep_for(std::chrono::milliseconds(1)); +		if (!check_pre && 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 -	retry("mkdir", [&] { +	retry_if("mkdir", [&] {  		std::filesystem::create_directories(fixed_path.parent_path()); // make sure folder exists  	}, [&] {  		return !std::filesystem::exists(fixed_path.parent_path());  	}); -	retry("touch file", [&] { +	retry_if("touch file", [&] {  		std::fstream(fixed_path, std::ios::trunc | std::ios::out).close(); // touch file  	}, [&] {  		return !std::filesystem::exists(fixed_path);  	});  	std::fstream* file = nullptr; -	retry("get file handle", [&] { +	retry_while("get file handle", [&] {  		delete file;  		file = new std::fstream(fixed_path.c_str(), std::ios::out | std::ios::in); // open file  	}, [&] { @@ -76,12 +77,11 @@ std::fstream* CacheManager::cache_get(const char* filename) {  	return file;  } -bool CacheManager::cache_exists(std::string filename) { return cache_exists(filename.c_str()); }  bool CacheManager::cache_exists(const char* filename) {  	return std::filesystem::exists(prefix_cache_path(filename));  } -  std::string CacheManager::prefix_cache_path(const char* filename) {  	return this->cache_path + "/" + std::string(filename);  } +  |