Skip to content

Commit

Permalink
more debugging for loading runners
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Apr 11, 2024
1 parent 10d358c commit d2ef532
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
42 changes: 31 additions & 11 deletions CloudController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,36 @@

namespace GithubActionsOrchestrator;

public class CloudController(
ILogger<CloudController> logger,
string hetznerCloudToken,
string persistPath,
List<MachineSize> configSizes)
public class CloudController
{
private readonly string _persistentPath = Path.Combine(persistPath, "activeRunners.json");
private readonly HetznerCloudClient _client = new(hetznerCloudToken);
private readonly ILogger _logger = logger;
private readonly string _persistentPath;
private readonly HetznerCloudClient _client;
private readonly ILogger _logger;
private List<Machine> _activeRunners = new();
private static readonly Gauge ActiveMachinesCount = Metrics
.CreateGauge("github_machines_active", "Number of active machines", labelNames: ["org","size"]);

private readonly List<MachineSize> _configSizes;

public CloudController(ILogger<CloudController> logger,
string hetznerCloudToken,
string persistPath,
List<MachineSize> configSizes)
{
_configSizes = configSizes;
_persistentPath = Path.Combine(persistPath, "activeRunners.json");
_client = new(hetznerCloudToken);
_logger = logger;
_logger.LogInformation("Loading from persistent file.");
LoadActiveRunners().Wait();
_logger.LogInformation("Controller init done.");
}

public async Task<string> CreateNewRunner(string arch, string size, string runnerToken, string orgName)
{

// Select VM size for job - All AMD
string? vmSize = configSizes.FirstOrDefault(x => x.Arch == arch && x.Name == size)?.VmType;
string? vmSize = _configSizes.FirstOrDefault(x => x.Arch == arch && x.Name == size)?.VmType;

if (string.IsNullOrEmpty(vmSize))
{
Expand Down Expand Up @@ -113,11 +125,19 @@ private void StoreActiveRunners()

public async Task LoadActiveRunners()
{
if (!File.Exists(_persistentPath)) return;
if (!File.Exists(_persistentPath))
{
_logger.LogWarning($"No active runner file found at {_persistentPath}");
return;
}
string json = await File.ReadAllTextAsync(_persistentPath);
var restoredRunners = JsonSerializer.Deserialize<List<Machine>>(json);

if (restoredRunners == null) 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");

Expand Down
4 changes: 1 addition & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ public static void Main(string[] args)
builder.Services.AddSingleton<CloudController>(svc =>
{
var logger = svc.GetRequiredService<ILogger<CloudController>>();
var cc = new CloudController(logger, Config.HetznerToken, persistPath,Config.Sizes);
cc.LoadActiveRunners().Wait();
return cc;
return new CloudController(logger, Config.HetznerToken, persistPath,Config.Sizes);
});

var app = builder.Build();
Expand Down

0 comments on commit d2ef532

Please sign in to comment.