Skip to content

Commit

Permalink
make persistant more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Apr 11, 2024
1 parent d2ef532 commit 7c676e6
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions CloudController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,15 @@ public async Task<string> 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 })
Expand Down Expand Up @@ -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<List<Machine>>(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<List<Machine>>(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();

}
Expand Down

0 comments on commit 7c676e6

Please sign in to comment.