GiantsTools/Giants.Services/Services/ServerRegistryService.cs

47 lines
1.7 KiB
C#
Raw Normal View History

namespace Giants.Services
{
using System;
using System.Collections.Generic;
2020-08-09 01:31:16 +02:00
using System.Linq;
using System.Threading.Tasks;
2020-08-09 01:31:16 +02:00
using Giants.Services.Core;
using Microsoft.Extensions.Configuration;
public class ServerRegistryService : IServerRegistryService
{
private readonly IServerRegistryStore registryStore;
2020-08-09 01:31:16 +02:00
private readonly IConfiguration configuration;
private readonly IDateTimeProvider dateTimeProvider;
private readonly TimeSpan timeoutPeriod;
private readonly int maxServerCount;
2020-08-09 01:31:16 +02:00
public ServerRegistryService(
IServerRegistryStore registryStore,
IConfiguration configuration,
IDateTimeProvider dateTimeProvider)
{
this.registryStore = registryStore;
2020-08-09 01:31:16 +02:00
this.configuration = configuration;
this.dateTimeProvider = dateTimeProvider;
this.timeoutPeriod = TimeSpan.FromMinutes(Convert.ToDouble(this.configuration["ServerTimeoutPeriodInMinutes"]));
this.maxServerCount = Convert.ToInt32(this.configuration["MaxServerCount"]);
}
public async Task AddServer(
2020-08-09 02:52:26 +02:00
ServerInfo serverInfo)
{
2020-08-09 02:52:26 +02:00
ArgumentUtility.CheckForNull(serverInfo, nameof(serverInfo));
ArgumentUtility.CheckStringForNullOrEmpty(serverInfo.HostIpAddress, nameof(serverInfo.HostIpAddress));
2020-08-09 02:52:26 +02:00
await this.registryStore.UpsertServerInfo(serverInfo);
}
public async Task<IEnumerable<ServerInfo>> GetAllServers()
{
2020-08-09 01:31:16 +02:00
return (await this.registryStore
2020-08-09 02:26:41 +02:00
.GetServerInfos(whereExpression: c => c.LastHeartbeat > this.dateTimeProvider.UtcNow - this.timeoutPeriod))
2020-08-09 01:31:16 +02:00
.Take(this.maxServerCount);
}
}
}