2024-10-17 17:17:09 -04:00
|
|
|
using HermesSocketServer.Store;
|
|
|
|
|
|
|
|
namespace HermesSocketServer.Services
|
|
|
|
{
|
|
|
|
public class DatabaseService : BackgroundService
|
|
|
|
{
|
|
|
|
private readonly VoiceStore _voices;
|
2024-10-17 23:21:16 -04:00
|
|
|
private readonly ChatterStore _chatters;
|
|
|
|
private readonly ServerConfiguration _configuration;
|
2024-10-17 17:17:09 -04:00
|
|
|
private readonly Serilog.ILogger _logger;
|
|
|
|
|
2024-10-17 23:21:16 -04:00
|
|
|
public DatabaseService(VoiceStore voices, ChatterStore chatters, ServerConfiguration configuration, Serilog.ILogger logger) {
|
2024-10-17 17:17:09 -04:00
|
|
|
_voices = voices;
|
2024-10-17 23:21:16 -04:00
|
|
|
_chatters = chatters;
|
|
|
|
_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();
|
2024-10-17 23:21:16 -04:00
|
|
|
_logger.Information("Loading TTS chatters' voice.");
|
|
|
|
await _chatters.Load();
|
2024-10-17 17:17:09 -04:00
|
|
|
|
|
|
|
await Task.Run(async () =>
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2024-10-17 23:21:16 -04:00
|
|
|
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
2024-10-17 17:17:09 -04:00
|
|
|
await _voices.Save();
|
2024-10-17 23:21:16 -04:00
|
|
|
await _chatters.Save();
|
2024-10-17 17:17:09 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|