Warrior_EA/Database/DatabaseVersionManager.mqh

63 lines
2.9 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:35:54 +02:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CDatabaseVersionManager
{
public:
string ReadStoredDbVersion(string versionFilePath)
{
if(!FileIsExist(versionFilePath, FILE_COMMON))
return "NA";
int fileHandle = INVALID_HANDLE; // Initialize to a safe value
int attempts = 0;
while(attempts < 5)
{
// 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.
fileHandle = FileOpen(versionFilePath, FILE_COMMON | FILE_READ | FILE_SHARE_READ | FILE_SHARE_WRITE);
2025-05-30 16:35:54 +02:00
if(fileHandle != INVALID_HANDLE)
{
break; // Success, break out of the loop
}
Print("Retry " + IntegerToString(attempts + 1) + " failed to open file for reading: " + versionFilePath + " (error " + IntegerToString(GetLastError()) + ")");
2025-05-30 16:35:54 +02:00
Sleep(1000); // Wait for a second before retrying
attempts++;
}
if(fileHandle == INVALID_HANDLE)
{
Print("Failed to open file for reading after retries: " + versionFilePath + " (error " + IntegerToString(GetLastError()) + ")");
2025-05-30 16:35:54 +02:00
return "ERROR";
}
string dbVersion = FileReadString(fileHandle);
FileClose(fileHandle);
return dbVersion;
}
bool UpdateStoredDbVersion(string versionFilePath, string dbVersion)
{
int fileHandle = INVALID_HANDLE; // Initialize to a safe value
int attempts = 0;
while(attempts < 5)
{
fileHandle = FileOpen(versionFilePath, FILE_COMMON | FILE_WRITE | FILE_SHARE_READ | FILE_SHARE_WRITE);
2025-05-30 16:35:54 +02:00
if(fileHandle != INVALID_HANDLE)
{
break; // Success, break out of the loop
}
Print("Retry " + IntegerToString(attempts + 1) + " failed to open file for writing: " + versionFilePath + " (error " + IntegerToString(GetLastError()) + ")");
2025-05-30 16:35:54 +02:00
Sleep(1000); // Wait for a second before retrying
attempts++;
}
if(fileHandle == INVALID_HANDLE)
{
Print("Failed to open file for writing after retries: " + versionFilePath + " (error " + IntegerToString(GetLastError()) + ")");
2025-05-30 16:35:54 +02:00
return false;
}
FileWriteString(fileHandle, dbVersion);
FileClose(fileHandle);
return true;
}
};
//+------------------------------------------------------------------+