hermes-server/Services/DatabaseService.cs

46 lines
1.5 KiB
C#

using HermesSocketLibrary.db;
using HermesSocketServer.Store;
namespace HermesSocketServer.Services
{
public class DatabaseService : BackgroundService
{
private readonly ChannelManager _channels;
private readonly VoiceStore _voices;
private readonly UserStore _users;
private readonly ServerConfiguration _configuration;
private readonly Serilog.ILogger _logger;
public DatabaseService(ChannelManager channels, VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger)
{
_channels = channels;
_voices = voices;
_users = users;
_configuration = configuration;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
_logger.Information("Loading TTS voices...");
await _voices.Load();
_logger.Information("Loading users...");
await _users.Load();
await Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
while (true)
{
await Task.WhenAll([
_voices.Save(),
_users.Save(),
_channels.Save(),
]);
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
}
});
}
}
}