102 lines
3.9 KiB
C#
102 lines
3.9 KiB
C#
using MareSynchronos.API.Dto;
|
|
using MareSynchronos.API.SignalR;
|
|
using MareSynchronosServer.Hubs;
|
|
using MareSynchronosShared.Data;
|
|
using MareSynchronosShared.Metrics;
|
|
using MareSynchronosShared.Services;
|
|
using MareSynchronosShared.Utils.Configuration;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using StackExchange.Redis;
|
|
using StackExchange.Redis.Extensions.Core.Abstractions;
|
|
|
|
namespace MareSynchronosServer.Services;
|
|
|
|
public class SystemInfoService : IHostedService, IDisposable
|
|
{
|
|
private readonly MareMetrics _mareMetrics;
|
|
private readonly IConfigurationService<ServerConfiguration> _config;
|
|
private readonly IServiceProvider _services;
|
|
private readonly ILogger<SystemInfoService> _logger;
|
|
private readonly IHubContext<MareHub, IMareHub> _hubContext;
|
|
private readonly IRedisDatabase _redis;
|
|
private readonly IConnectionMultiplexer _connectionMultiplexer;
|
|
private Timer _timer;
|
|
public SystemInfoDto SystemInfoDto { get; private set; } = new();
|
|
|
|
public SystemInfoService(MareMetrics mareMetrics, IConfigurationService<ServerConfiguration> configurationService, IServiceProvider services,
|
|
ILogger<SystemInfoService> logger, IHubContext<MareHub, IMareHub> hubContext, IRedisDatabase redisDb)
|
|
{
|
|
_mareMetrics = mareMetrics;
|
|
_config = configurationService;
|
|
_services = services;
|
|
_logger = logger;
|
|
_hubContext = hubContext;
|
|
_redis = redisDb;
|
|
_connectionMultiplexer = _redis.Database.Multiplexer;
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("System Info Service started");
|
|
|
|
var timeOut = _config.IsMain ? 5 : 15;
|
|
|
|
_timer = new Timer(PushSystemInfo, null, TimeSpan.Zero, TimeSpan.FromSeconds(timeOut));
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void PushSystemInfo(object state)
|
|
{
|
|
try
|
|
{
|
|
ThreadPool.GetAvailableThreads(out int workerThreads, out int ioThreads);
|
|
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeAvailableWorkerThreads, workerThreads);
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeAvailableIOWorkerThreads, ioThreads);
|
|
var onlineUsers = _connectionMultiplexer.GetServers().Where(server => !server.IsReplica).SelectMany(server => server.Keys(0, "UID:*", pageSize:1000)).Count();
|
|
|
|
|
|
SystemInfoDto = new SystemInfoDto()
|
|
{
|
|
OnlineUsers = onlineUsers,
|
|
};
|
|
|
|
if (_config.IsMain)
|
|
{
|
|
_logger.LogTrace("Sending System Info, Online Users: {onlineUsers}", onlineUsers);
|
|
|
|
_ = _hubContext.Clients.All.Client_UpdateSystemInfo(SystemInfoDto);
|
|
|
|
using var scope = _services.CreateScope();
|
|
using var db = scope.ServiceProvider.GetService<MareDbContext>()!;
|
|
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeAuthorizedConnections, onlineUsers);
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugePairs, db.ClientPairs.AsNoTracking().Count());
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugePairsPaused, db.ClientPairs.AsNoTracking().Count(p => p.IsPaused));
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroups, db.Groups.AsNoTracking().Count());
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairs, db.GroupPairs.AsNoTracking().Count());
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairsPaused, db.GroupPairs.AsNoTracking().Count(p => p.IsPaused));
|
|
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeUsersRegistered, db.Users.AsNoTracking().Count());
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to push system info");
|
|
}
|
|
}
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
_timer?.Change(Timeout.Infinite, 0);
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_timer?.Dispose();
|
|
}
|
|
} |