Add load balancing

This commit is contained in:
Stone_Red
2025-12-20 18:46:21 +01:00
parent 7c3ab3a154
commit 27558ba3f2
10 changed files with 322 additions and 46 deletions
@@ -0,0 +1,36 @@
using Microsoft.AspNetCore.SignalR;
using RemoteExec.Server.Hubs;
namespace RemoteExec.Server.Services;
public class MetricsBroadcastService(IHubContext<RemoteExecutionHub> hubContext, ILogger<MetricsBroadcastService> logger) : BackgroundService
{
private readonly TimeSpan _broadcastInterval = TimeSpan.FromSeconds(2);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Metrics broadcast service started");
while (!stoppingToken.IsCancellationRequested)
{
try
{
await Task.Delay(_broadcastInterval, stoppingToken);
await RemoteExecutionHub.BroadcastMetricsAsync(hubContext);
}
catch (OperationCanceledException)
{
// Expected when service is stopping
break;
}
catch (Exception ex)
{
logger.LogError(ex, "Error broadcasting metrics");
}
}
logger.LogInformation("Metrics broadcast service stopped");
}
}