fix(agent): settings distants réellement appliqués (heartbeat live)

ConfigReceiverService.ApplySetting ne faisait que logger. Le setting
heartbeat.interval.seconds est désormais appliqué à chaud : écrit dans
AgentState.HeartbeatIntervalSeconds (snapshot thread-safe) et relu par
AgentRegistrationService à chaque cycle — plus besoin de redémarrer
l'agent pour changer l'intervalle. Validation 5-300s, valeurs invalides
ignorées sans muter l'état.

Tests : +2 (application live vérifiée, invalides ne mutent pas l'état).
This commit is contained in:
Arnaud Dartois committed 2026-08-10 11:02:37 +02:00
1 parent b6d4164652
commit 3ff20ddf98
4 files changed
+60 -9

No files matched your search

@@ -142,8 +142,9 @@ public class AgentRegistrationService : BackgroundService
}
// Step 2: Heartbeat loop
var heartbeatInterval = TimeSpan.FromSeconds(_options.HeartbeatIntervalSeconds);
_logger.LogInformation("Starting heartbeat loop ({Interval}s interval)", _options.HeartbeatIntervalSeconds);
var heartbeatInterval = TimeSpan.FromSeconds(
_state.HeartbeatIntervalSeconds ?? _options.HeartbeatIntervalSeconds);
_logger.LogInformation("Starting heartbeat loop ({Interval}s interval)", heartbeatInterval.TotalSeconds);
while (!stoppingToken.IsCancellationRequested)
{
@@ -170,6 +171,11 @@ public class AgentRegistrationService : BackgroundService
_logger.LogWarning(ex, "Heartbeat error (will retry)");
}
// Re-read the interval each cycle so a remote setting change
// (ConfigReceiverService → AgentState.HeartbeatIntervalSeconds)
// takes effect without a restart.
heartbeatInterval = TimeSpan.FromSeconds(
_state.HeartbeatIntervalSeconds ?? _options.HeartbeatIntervalSeconds);
await Task.Delay(heartbeatInterval, stoppingToken);
}
}
+17 -2
View File
@@ -14,9 +14,14 @@ public sealed class AgentState
private string _stateDir = "/var/lib/heimdall";
private AgentSnapshot _snapshot = AgentSnapshot.Default;
private sealed record AgentSnapshot(Guid AgentId, string? ApiKey, string? ApiKeyPrefix, string? PublicIp)
private sealed record AgentSnapshot(
Guid AgentId,
string? ApiKey,
string? ApiKeyPrefix,
string? PublicIp,
int? HeartbeatIntervalSeconds)
{
public static readonly AgentSnapshot Default = new(Guid.Empty, null, null, null);
public static readonly AgentSnapshot Default = new(Guid.Empty, null, null, null, null);
}
/// <summary>
@@ -53,6 +58,16 @@ public sealed class AgentState
set => Interlocked.Exchange(ref _snapshot, _snapshot with { PublicIp = value });
}
/// <summary>
/// Heartbeat interval in seconds, as set remotely by Central via the
/// config hub. Null = use the local AgentOptions default.
/// </summary>
public int? HeartbeatIntervalSeconds
{
get => _snapshot.HeartbeatIntervalSeconds;
set => Interlocked.Exchange(ref _snapshot, _snapshot with { HeartbeatIntervalSeconds = value });
}
/// <summary>
/// Loads a previously saved identity, or returns null if none exists.
/// </summary>
@@ -123,14 +123,18 @@ public sealed class ConfigReceiverService : BackgroundService
private void ApplySetting(string key, string value)
{
// Apply known settings locally
// Apply known settings locally (live, no restart required)
switch (key)
{
case "heartbeat.interval.seconds":
if (int.TryParse(value, out var interval) && interval >= 5)
if (int.TryParse(value, out var interval) && interval >= 5 && interval <= 300)
{
// The heartbeat interval will be read from options on next heartbeat
_logger.LogInformation("Heartbeat interval updated to {Interval}s (applied on next restart)", interval);
_state.HeartbeatIntervalSeconds = interval;
_logger.LogInformation("Heartbeat interval updated to {Interval}s (applied live)", interval);
}
else
{
_logger.LogWarning("Ignored invalid heartbeat interval: {Value}", value);
}
break;
case "log.level":
@@ -110,13 +110,39 @@ public class ConfigReceiverServiceTests
[Fact]
public void ApplySetting_ValidHeartbeatInterval_DoesNotThrow()
{
var service = CreateService();
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
var service = CreateService(state);
var method = typeof(ConfigReceiverService).GetMethod("ApplySetting",
BindingFlags.NonPublic | BindingFlags.Instance);
method.ShouldNotBeNull();
Record.Exception(() => method.Invoke(service, ["heartbeat.interval.seconds", "30"]))
.ShouldBeNull();
// The setting is applied live to the shared state
state.HeartbeatIntervalSeconds.ShouldBe(30);
}
[Fact]
public void ApplySetting_InvalidHeartbeatInterval_DoesNotMutateState()
{
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
var service = CreateService(state);
var method = typeof(ConfigReceiverService).GetMethod("ApplySetting",
BindingFlags.NonPublic | BindingFlags.Instance);
method.ShouldNotBeNull();
// Below minimum (5)
method.Invoke(service, ["heartbeat.interval.seconds", "3"]);
state.HeartbeatIntervalSeconds.ShouldBeNull();
// Above maximum (300)
method.Invoke(service, ["heartbeat.interval.seconds", "999"]);
state.HeartbeatIntervalSeconds.ShouldBeNull();
// Non-numeric
method.Invoke(service, ["heartbeat.interval.seconds", "abc"]);
state.HeartbeatIntervalSeconds.ShouldBeNull();
}
[Fact]