rework configuration save, load configuration backups when available and config cannot be read
fix unnecessary config reload on save
This commit is contained in:
@@ -3,27 +3,28 @@ using System.Text.Json;
|
||||
|
||||
namespace MareSynchronos.MareConfiguration;
|
||||
|
||||
public abstract class ConfigurationServiceBase<T> : IDisposable where T : IMareConfiguration
|
||||
public abstract class ConfigurationServiceBase<T> : IConfigService<T> where T : IMareConfiguration
|
||||
{
|
||||
private readonly CancellationTokenSource _periodicCheckCts = new();
|
||||
private bool _configIsDirty = false;
|
||||
private DateTime _configLastWriteTime;
|
||||
private Lazy<T> _currentConfigInternal;
|
||||
private bool _disposed = false;
|
||||
|
||||
protected ConfigurationServiceBase(string configurationDirectory)
|
||||
public event EventHandler? ConfigSave;
|
||||
|
||||
protected ConfigurationServiceBase(string configDirectory)
|
||||
{
|
||||
ConfigurationDirectory = configurationDirectory;
|
||||
ConfigurationDirectory = configDirectory;
|
||||
|
||||
_ = Task.Run(CheckForConfigUpdatesInternal, _periodicCheckCts.Token);
|
||||
_ = Task.Run(CheckForDirtyConfigInternal, _periodicCheckCts.Token);
|
||||
|
||||
_currentConfigInternal = LazyConfig();
|
||||
}
|
||||
|
||||
public string ConfigurationDirectory { get; init; }
|
||||
public T Current => _currentConfigInternal.Value;
|
||||
protected abstract string ConfigurationName { get; }
|
||||
protected string ConfigurationPath => Path.Combine(ConfigurationDirectory, ConfigurationName);
|
||||
public abstract string ConfigurationName { get; }
|
||||
public string ConfigurationPath => Path.Combine(ConfigurationDirectory, ConfigurationName);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -33,14 +34,20 @@ public abstract class ConfigurationServiceBase<T> : IDisposable where T : IMareC
|
||||
|
||||
public void Save()
|
||||
{
|
||||
_configIsDirty = true;
|
||||
ConfigSave?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void UpdateLastWriteTime()
|
||||
{
|
||||
_configLastWriteTime = GetConfigLastWriteTime();
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposing || _disposed) return;
|
||||
_disposed = true;
|
||||
_periodicCheckCts.Cancel();
|
||||
_periodicCheckCts.Dispose();
|
||||
if (_configIsDirty) SaveDirtyConfig();
|
||||
}
|
||||
|
||||
protected T LoadConfig()
|
||||
@@ -48,8 +55,12 @@ public abstract class ConfigurationServiceBase<T> : IDisposable where T : IMareC
|
||||
T? config;
|
||||
if (!File.Exists(ConfigurationPath))
|
||||
{
|
||||
config = (T)Activator.CreateInstance(typeof(T))!;
|
||||
Save();
|
||||
config = AttemptToLoadBackup();
|
||||
if (Equals(config, default(T)))
|
||||
{
|
||||
config = (T)Activator.CreateInstance(typeof(T))!;
|
||||
Save();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -60,9 +71,10 @@ public abstract class ConfigurationServiceBase<T> : IDisposable where T : IMareC
|
||||
catch
|
||||
{
|
||||
// config failed to load for some reason
|
||||
config = default;
|
||||
config = AttemptToLoadBackup();
|
||||
}
|
||||
if (config == null)
|
||||
|
||||
if (config == null || Equals(config, default(T)))
|
||||
{
|
||||
config = (T)Activator.CreateInstance(typeof(T))!;
|
||||
Save();
|
||||
@@ -73,35 +85,36 @@ public abstract class ConfigurationServiceBase<T> : IDisposable where T : IMareC
|
||||
return config;
|
||||
}
|
||||
|
||||
protected void SaveDirtyConfig()
|
||||
private T? AttemptToLoadBackup()
|
||||
{
|
||||
_configIsDirty = false;
|
||||
var existingConfigs = Directory.EnumerateFiles(ConfigurationDirectory, ConfigurationName + ".bak.*").Select(c => new FileInfo(c))
|
||||
.OrderByDescending(c => c.LastWriteTime).ToList();
|
||||
if (existingConfigs.Skip(10).Any())
|
||||
var configBackupFolder = Path.Join(ConfigurationDirectory, ConfigurationSaveService.BackupFolder);
|
||||
var configNameSplit = ConfigurationName.Split(".");
|
||||
if (!Directory.Exists(configBackupFolder))
|
||||
return default;
|
||||
|
||||
var existingBackups = Directory.EnumerateFiles(configBackupFolder, configNameSplit[0] + "*").OrderByDescending(f => new FileInfo(f).LastWriteTimeUtc);
|
||||
foreach (var file in existingBackups)
|
||||
{
|
||||
foreach (var config in existingConfigs.Skip(10).ToList())
|
||||
try
|
||||
{
|
||||
config.Delete();
|
||||
var config = JsonSerializer.Deserialize<T>(File.ReadAllText(file));
|
||||
if (Equals(config, default(T)))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
|
||||
File.Copy(file, ConfigurationPath, true);
|
||||
return config;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// couldn't load backup, might as well delete it
|
||||
File.Delete(file);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
File.Copy(ConfigurationPath, ConfigurationPath + ".bak." + DateTime.Now.ToString("yyyyMMddHHmmss"), overwrite: true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore if file cannot be backupped once
|
||||
}
|
||||
|
||||
var temp = ConfigurationPath + ".tmp";
|
||||
File.WriteAllText(temp, JsonSerializer.Serialize(Current, new JsonSerializerOptions()
|
||||
{
|
||||
WriteIndented = true
|
||||
}));
|
||||
File.Move(temp, ConfigurationPath, true);
|
||||
_configLastWriteTime = new FileInfo(ConfigurationPath).LastWriteTimeUtc;
|
||||
return default;
|
||||
}
|
||||
|
||||
private async Task CheckForConfigUpdatesInternal()
|
||||
@@ -118,20 +131,12 @@ public abstract class ConfigurationServiceBase<T> : IDisposable where T : IMareC
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CheckForDirtyConfigInternal()
|
||||
private DateTime GetConfigLastWriteTime()
|
||||
{
|
||||
while (!_periodicCheckCts.IsCancellationRequested)
|
||||
{
|
||||
if (_configIsDirty)
|
||||
{
|
||||
SaveDirtyConfig();
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), _periodicCheckCts.Token).ConfigureAwait(false);
|
||||
}
|
||||
try { return new FileInfo(ConfigurationPath).LastWriteTimeUtc; }
|
||||
catch { return DateTime.MinValue; }
|
||||
}
|
||||
|
||||
private DateTime GetConfigLastWriteTime() => new FileInfo(ConfigurationPath).LastWriteTimeUtc;
|
||||
|
||||
private Lazy<T> LazyConfig()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user