Skip to content

Commit

Permalink
reload config when config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Jan 28, 2025
1 parent 9342181 commit cd4695c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 28 deletions.
4 changes: 2 additions & 2 deletions CloudController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public async Task<Machine> CreateNewRunner(string arch, string size, string runn
bool success = false;
List<eDataCenter> dataCenters =
[
eDataCenter.nbg1,
eDataCenter.fsn1,
eDataCenter.hel1,
eDataCenter.nbg1
eDataCenter.hel1
];

int ct = 0;
Expand Down
4 changes: 4 additions & 0 deletions PoolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

if (DateTime.UtcNow - crudeStatsTimer > TimeSpan.FromSeconds(statsSeconds))
{
// Update config
Program.LoadConfiguration();

// Grab some stats
await ProcessStats(targetConfig);
crudeStatsTimer = DateTime.UtcNow;
Expand All @@ -77,6 +80,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
// check for culling interval
if (DateTime.UtcNow - crudeTimer > TimeSpan.FromMinutes(cullMinutes))
{

_logger.LogInformation("Cleaning runners...");
await CleanUpRunners(targetConfig);
await StartPoolRunners(targetConfig);
Expand Down
91 changes: 65 additions & 26 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using GithubActionsOrchestrator.Database;
Expand Down Expand Up @@ -49,36 +51,12 @@ public static void Main(string[] args)
.CreateLogger();


string configDir = Environment.GetEnvironmentVariable("CONFIG_DIR") ??
Directory.CreateTempSubdirectory().FullName;

// Setup pool config
string configPath = Path.Combine(configDir, "config.json");
if (!File.Exists(configPath))
{
Log.Error($"Unable to read config file at {configPath}");
return;
}

string configJson = File.ReadAllText(configPath);
JsonSerializerOptions options = new()
{
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
}
};
Config = JsonSerializer.Deserialize<AutoScalerConfiguration>(configJson, options) ??
throw new Exception("Unable to parse configuration");

if (string.IsNullOrWhiteSpace(Config.HetznerToken))
if (!LoadConfiguration())
{
Log.Error($"Hetzner cloud token not set in {configPath}");
Log.Error("Unable to do initial config load. Aborting.");
return;
}


Log.Information($"Loaded {Config.TargetConfigs.Count} targets and {Config.Sizes.Count} sizes.");

// Prepare metrics
using KestrelMetricServer server = new(port: 9000);
Expand Down Expand Up @@ -153,6 +131,67 @@ public static void Main(string[] args)
app.Run(Program.Config.ListenUrl);
}

static string GetFileHash(string filePath)
{
using SHA256 sha256 = SHA256.Create();
// Open the file and compute the hash
using FileStream stream = File.OpenRead(filePath);
byte[] hashBytes = sha256.ComputeHash(stream);
// Convert the byte array to a hexadecimal string
StringBuilder hashString = new StringBuilder();
foreach (byte b in hashBytes)
{
hashString.Append(b.ToString("x2")); // Convert each byte to a hexadecimal string
}
return hashString.ToString();
}


public static bool LoadConfiguration()
{
string configDir = Environment.GetEnvironmentVariable("CONFIG_DIR") ??
Directory.CreateTempSubdirectory().FullName;
// Setup pool config
string configPath = Path.Combine(configDir, "config.json");
if (!File.Exists(configPath))
{
Log.Error($"Unable to locate config file at {configPath}");
return false;
}
string configFileHash = GetFileHash(configPath);

if (configFileHash == LoadedConfigHash)
{
return true;
}

Log.Information("Loading/refreshing configuration...");

string configJson = File.ReadAllText(configPath);
JsonSerializerOptions options = new()
{
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
}
};
var loadedConfig = JsonSerializer.Deserialize<AutoScalerConfiguration>(configJson, options) ??
throw new Exception("Unable to parse configuration");

if (string.IsNullOrWhiteSpace(loadedConfig.HetznerToken))
{
Log.Error($"Hetzner cloud token not set in {configPath}");
return false;
}

Config = loadedConfig;
LoadedConfigHash = configFileHash;
Log.Information($"Loaded {Config.TargetConfigs.Count} targets and {Config.Sizes.Count} sizes.");
return true;
}

public static string LoadedConfigHash { get; set; }

private static async Task<IResult> GithubWebhookHandler(HttpRequest request, [FromServices] CloudController cloud, [FromServices] ILogger<Program> logger, [FromServices] RunnerQueue poolMgr)
{
// Verify webhook HMAC - TODO
Expand Down

0 comments on commit cd4695c

Please sign in to comment.