fix(agent): wake heartbeat loop when interval changes
Heimdall Gitea / Build and tests (pull_request) Failing after 4m55s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and tests (pull_request) Failing after 4m55s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
This commit is contained in:
1 parent
aea3b46f68
commit
d543eced8d
2 files changed
+32
-2
No files matched your search
@@ -143,7 +143,26 @@ public class AgentRegistrationService : BackgroundService
|
||||
// takes effect without a restart.
|
||||
heartbeatInterval = TimeSpan.FromSeconds(
|
||||
_state.HeartbeatIntervalSeconds ?? _options.HeartbeatIntervalSeconds);
|
||||
await Task.Delay(heartbeatInterval, stoppingToken);
|
||||
await WaitForNextHeartbeatAsync(heartbeatInterval, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task WaitForNextHeartbeatAsync(TimeSpan interval, CancellationToken stoppingToken)
|
||||
{
|
||||
using var waitCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
|
||||
var delay = Task.Delay(interval, waitCts.Token);
|
||||
var intervalChanged = _state.WaitForHeartbeatIntervalChangeAsync(waitCts.Token);
|
||||
|
||||
await Task.WhenAny(delay, intervalChanged).ConfigureAwait(false);
|
||||
waitCts.Cancel();
|
||||
|
||||
try
|
||||
{
|
||||
await Task.WhenAll(delay, intervalChanged).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// Either the interval elapsed or a config update woke the loop.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace Heimdall.Agent.Services;
|
||||
/// </remarks>
|
||||
public sealed class AgentState
|
||||
{
|
||||
private readonly SemaphoreSlim _heartbeatIntervalChanged = new(0, 1);
|
||||
/// <summary>Data Protection purpose used to encrypt/decrypt the API key.</summary>
|
||||
public const string KeyProtectionPurpose = "Heimdall.Agent.ApiKey";
|
||||
|
||||
@@ -116,9 +117,19 @@ public sealed class AgentState
|
||||
public int? HeartbeatIntervalSeconds
|
||||
{
|
||||
get => _snapshot.HeartbeatIntervalSeconds;
|
||||
set => Interlocked.Exchange(ref _snapshot, _snapshot with { HeartbeatIntervalSeconds = value });
|
||||
set
|
||||
{
|
||||
var previous = Interlocked.Exchange(ref _snapshot, _snapshot with { HeartbeatIntervalSeconds = value });
|
||||
if (previous.HeartbeatIntervalSeconds == value) return;
|
||||
|
||||
try { _heartbeatIntervalChanged.Release(); }
|
||||
catch (SemaphoreFullException) { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Waits until a remote heartbeat interval change needs to re-arm the send loop.</summary>
|
||||
public Task WaitForHeartbeatIntervalChangeAsync(CancellationToken ct) => _heartbeatIntervalChanged.WaitAsync(ct);
|
||||
|
||||
/// <summary>
|
||||
/// Loads a previously saved identity, or returns null if none exists or the
|
||||
/// state cannot be read. Legacy plaintext files are detected, kept in
|
||||
|
||||
Reference in new issue
Block a user