//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CDatabaseVersionManager { private: //--- Read/Update both open this FILE_COMMON version file with the same 5-attempt/1s-sleep retry, //--- differing only in the open flags and the "reading"/"writing" noun in the log line. // FILE_SHARE_READ|FILE_SHARE_WRITE required - this file is FILE_COMMON (shared across every // chart instance/symbol), so without share flags a second instance's open can be blocked by // the first's exclusive handle, exhausting these retries and (via the ERROR sentinel below) // potentially triggering a version-mismatch CleanDirectory() that wipes the shared DB folder. int OpenVersionFileWithRetry(string versionFilePath, int openFlags, string verb) { int fileHandle = INVALID_HANDLE; // Initialize to a safe value int attempts = 0; while(attempts < 5) { fileHandle = FileOpen(versionFilePath, openFlags); if(fileHandle != INVALID_HANDLE) { break; // Success, break out of the loop } Print("Retry " + IntegerToString(attempts + 1) + " failed to open file for " + verb + ": " + versionFilePath + " (error " + IntegerToString(GetLastError()) + ")"); Sleep(1000); // Wait for a second before retrying attempts++; } if(fileHandle == INVALID_HANDLE) Print("Failed to open file for " + verb + " after retries: " + versionFilePath + " (error " + IntegerToString(GetLastError()) + ")"); return fileHandle; } public: string ReadStoredDbVersion(string versionFilePath) { if(!FileIsExist(versionFilePath, FILE_COMMON)) return "NA"; int fileHandle = OpenVersionFileWithRetry(versionFilePath, FILE_COMMON | FILE_READ | FILE_SHARE_READ | FILE_SHARE_WRITE, "reading"); if(fileHandle == INVALID_HANDLE) return "ERROR"; string dbVersion = FileReadString(fileHandle); FileClose(fileHandle); return dbVersion; } bool UpdateStoredDbVersion(string versionFilePath, string dbVersion) { int fileHandle = OpenVersionFileWithRetry(versionFilePath, FILE_COMMON | FILE_WRITE | FILE_SHARE_READ | FILE_SHARE_WRITE, "writing"); if(fileHandle == INVALID_HANDLE) return false; FileWriteString(fileHandle, dbVersion); FileClose(fileHandle); return true; } }; //+------------------------------------------------------------------+