From 7c676e6bb0416399f2c79dda81758456dca796f3 Mon Sep 17 00:00:00 2001 From: Markus Keil Date: Thu, 11 Apr 2024 20:33:08 +0200 Subject: [PATCH] make persistant more robust --- CloudController.cs | 65 +++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/CloudController.cs b/CloudController.cs index 980f8d2..4d116d8 100644 --- a/CloudController.cs +++ b/CloudController.cs @@ -94,8 +94,15 @@ public async Task CreateNewRunner(string arch, string size, string runne private void StoreActiveRunners() { - byte[] json = JsonSerializer.SerializeToUtf8Bytes(_activeRunners); - File.WriteAllBytes(_persistentPath, json); + try + { + byte[] json = JsonSerializer.SerializeToUtf8Bytes(_activeRunners); + File.WriteAllBytes(_persistentPath, json); + } + catch (Exception ex) + { + _logger.LogError($"Unable to write to {_persistentPath}: {ex.Message}"); + } var grouped = _activeRunners .GroupBy(m => new { m.OrgName, m.Size }) @@ -125,34 +132,44 @@ private void StoreActiveRunners() public async Task LoadActiveRunners() { - if (!File.Exists(_persistentPath)) + try { - _logger.LogWarning($"No active runner file found at {_persistentPath}"); - return; - } - string json = await File.ReadAllTextAsync(_persistentPath); - var restoredRunners = JsonSerializer.Deserialize>(json); + if (!File.Exists(_persistentPath)) + { + _logger.LogWarning($"No active runner file found at {_persistentPath}"); + return; + } - if (restoredRunners == null) - { - _logger.LogWarning($"Unable to parse active runner file found at {_persistentPath}"); - return; - } - _activeRunners = restoredRunners; - _logger.LogInformation($"Loaded {restoredRunners.Count} runners from store"); + string json = await File.ReadAllTextAsync(_persistentPath); + var restoredRunners = JsonSerializer.Deserialize>(json); - var htzServers = await _client.Server.Get(); - - // Check if known srv are still in hetzner - foreach (var knownSrv in _activeRunners.ToList()) - { - if (htzServers.All(x => x.Name != knownSrv.Name)) + if (restoredRunners == null) { - // Hetzner server no longer existing - remove from list - _logger.LogWarning($"Cleaned {knownSrv.Name} from internal list"); - _activeRunners.Remove(knownSrv); + _logger.LogWarning($"Unable to parse active runner file found at {_persistentPath}"); + return; + } + + _activeRunners = restoredRunners; + _logger.LogInformation($"Loaded {restoredRunners.Count} runners from store"); + + var htzServers = await _client.Server.Get(); + + // Check if known srv are still in hetzner + foreach (var knownSrv in _activeRunners.ToList()) + { + if (htzServers.All(x => x.Name != knownSrv.Name)) + { + // Hetzner server no longer existing - remove from list + _logger.LogWarning($"Cleaned {knownSrv.Name} from internal list"); + _activeRunners.Remove(knownSrv); + } } } + catch (Exception ex) + { + _logger.LogError($"Unable to load {_persistentPath}: {ex.Message}"); + } + StoreActiveRunners(); }