From 3ff20ddf987989b683c6d545ec20b8975b180ae5 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Mon, 10 Aug 2026 11:02:37 +0200 Subject: [PATCH] =?UTF-8?q?fix(agent):=20settings=20distants=20r=C3=A9elle?= =?UTF-8?q?ment=20appliqu=C3=A9s=20(heartbeat=20live)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- .../Services/AgentRegistrationService.cs | 10 +++++-- src/Heimdall.Agent/Services/AgentState.cs | 19 +++++++++++-- .../Services/ConfigReceiverService.cs | 12 +++++--- .../ConfigReceiverServiceTests.cs | 28 ++++++++++++++++++- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/Heimdall.Agent/Services/AgentRegistrationService.cs b/src/Heimdall.Agent/Services/AgentRegistrationService.cs index ae58acb..f966e79 100644 --- a/src/Heimdall.Agent/Services/AgentRegistrationService.cs +++ b/src/Heimdall.Agent/Services/AgentRegistrationService.cs @@ -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); } } diff --git a/src/Heimdall.Agent/Services/AgentState.cs b/src/Heimdall.Agent/Services/AgentState.cs index a283e84..7c91109 100644 --- a/src/Heimdall.Agent/Services/AgentState.cs +++ b/src/Heimdall.Agent/Services/AgentState.cs @@ -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); } /// @@ -53,6 +58,16 @@ public sealed class AgentState set => Interlocked.Exchange(ref _snapshot, _snapshot with { PublicIp = value }); } + /// + /// Heartbeat interval in seconds, as set remotely by Central via the + /// config hub. Null = use the local AgentOptions default. + /// + public int? HeartbeatIntervalSeconds + { + get => _snapshot.HeartbeatIntervalSeconds; + set => Interlocked.Exchange(ref _snapshot, _snapshot with { HeartbeatIntervalSeconds = value }); + } + /// /// Loads a previously saved identity, or returns null if none exists. /// diff --git a/src/Heimdall.Agent/Services/ConfigReceiverService.cs b/src/Heimdall.Agent/Services/ConfigReceiverService.cs index 30ea963..2296077 100644 --- a/src/Heimdall.Agent/Services/ConfigReceiverService.cs +++ b/src/Heimdall.Agent/Services/ConfigReceiverService.cs @@ -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": diff --git a/tests/Heimdall.Agent.Tests/ConfigReceiverServiceTests.cs b/tests/Heimdall.Agent.Tests/ConfigReceiverServiceTests.cs index f095c31..8973af2 100644 --- a/tests/Heimdall.Agent.Tests/ConfigReceiverServiceTests.cs +++ b/tests/Heimdall.Agent.Tests/ConfigReceiverServiceTests.cs @@ -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]