Improve I/O database code

This should lead to less file corruptions.
This commit is contained in:
JustArchi
2017-07-07 07:33:31 +02:00
parent c4e7bbbf0c
commit 589f4f5746
2 changed files with 12 additions and 28 deletions

View File

@@ -26,7 +26,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using Newtonsoft.Json;
namespace ArchiSteamFarm {
@@ -152,21 +151,14 @@ namespace ArchiSteamFarm {
return;
}
// This call verifies if JSON is alright
// We don't wrap it in try catch as it should always be the case
// And if it's not, we want to know about it (in a crash) and correct it in future version
JsonConvert.DeserializeObject<BotDatabase>(json);
lock (FileLock) {
for (byte i = 0; i < 5; i++) {
try {
File.WriteAllText(FilePath, json);
break;
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
string newFilePath = FilePath + ".new";
Thread.Sleep(1000);
try {
File.WriteAllText(newFilePath, json);
File.Replace(newFilePath, FilePath, null);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
}
}

View File

@@ -25,7 +25,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Newtonsoft.Json;
namespace ArchiSteamFarm {
@@ -114,21 +113,14 @@ namespace ArchiSteamFarm {
return;
}
// This call verifies if JSON is alright
// We don't wrap it in try catch as it should always be the case
// And if it's not, we want to know about it (in a crash) and correct it in future version
JsonConvert.DeserializeObject<GlobalDatabase>(json, CustomSerializerSettings);
lock (FileLock) {
for (byte i = 0; i < 5; i++) {
try {
File.WriteAllText(FilePath, json);
break;
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
string newFilePath = FilePath + ".new";
Thread.Sleep(1000);
try {
File.WriteAllText(newFilePath, json);
File.Replace(newFilePath, FilePath, null);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
}
}