Files
Heimdall/src/Heimdall.Api/Hubs/ConfigHub.cs
T

97 lines
3.5 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Heimdall.Application.Services;
namespace Heimdall.Api.Hubs;
/// <summary>
/// SignalR hub for real-time configuration push to agents.
/// Agents connect and receive config updates instantly when settings change.
/// </summary>
[Authorize(AuthenticationSchemes = "ApiKey")]
public class ConfigHub : Hub
{
private readonly ILogger<ConfigHub> _logger;
public ConfigHub(ILogger<ConfigHub> logger)
{
_logger = logger;
}
/// <summary>
/// Agents call this on connect to join their personal config channel.
/// The agentId comes from the authenticated API key's associated agent.
/// </summary>
public async Task JoinAgentGroup(string agentId)
{
if (!Guid.TryParse(agentId, out var _))
{
await Clients.Caller.SendAsync("Error", "Invalid agentId format");
return;
}
await Groups.AddToGroupAsync(Context.ConnectionId, $"agent_{agentId}");
_logger.LogInformation("Agent {AgentId} joined config group", agentId);
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
_logger.LogInformation("Agent disconnected from config hub: {ConnectionId}", Context.ConnectionId);
await base.OnDisconnectedAsync(exception);
}
}
/// <summary>
/// Service that broadcasts config changes to connected agents via SignalR.
/// Called by the SettingService after any setting change.
/// </summary>
public interface IConfigBroadcastService
{
/// <summary>Notify all connected agents that a global setting changed.</summary>
Task NotifyGlobalSettingChanged(string key, string value, CancellationToken ct = default);
/// <summary>Notify a specific agent that one of its overrides changed.</summary>
Task NotifyAgentSettingChanged(Guid agentId, string key, string value, CancellationToken ct = default);
/// <summary>Notify all agents to reload their full configuration.</summary>
Task NotifyAllAgentsReload(CancellationToken ct = default);
}
/// <summary>
/// Broadcasts config changes to connected agents via SignalR.
/// Injected as a scoped service; the HubContext is a singleton so broadcasting
/// works safely across all connected clients.
/// </summary>
public class ConfigBroadcastService : IConfigBroadcastService
{
private readonly IHubContext<ConfigHub> _hubContext;
private readonly ILogger<ConfigBroadcastService> _logger;
public ConfigBroadcastService(IHubContext<ConfigHub> hubContext, ILogger<ConfigBroadcastService> logger)
{
_hubContext = hubContext;
_logger = logger;
}
/// <inheritdoc />
public async Task NotifyGlobalSettingChanged(string key, string value, CancellationToken ct = default)
{
_logger.LogDebug("Broadcasting global setting change: {Key} = {Value}", key, value);
await _hubContext.Clients.All.SendAsync("SettingChanged", key, value, ct);
}
/// <inheritdoc />
public async Task NotifyAgentSettingChanged(Guid agentId, string key, string value, CancellationToken ct = default)
{
_logger.LogDebug("Broadcasting agent setting change: Agent={AgentId}, {Key} = {Value}", agentId, key, value);
await _hubContext.Clients.Group($"agent_{agentId}").SendAsync("SettingChanged", key, value, ct);
}
/// <inheritdoc />
public async Task NotifyAllAgentsReload(CancellationToken ct = default)
{
_logger.LogInformation("Broadcasting full config reload to all agents");
await _hubContext.Clients.All.SendAsync("ConfigReload", ct);
}
}