hermes-server/Services/DatabaseService.cs

45 lines
1.5 KiB
C#
Raw Normal View History

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