2024-10-21 16:44:20 -04:00
|
|
|
using HermesSocketLibrary.db;
|
2024-10-17 17:17:09 -04:00
|
|
|
using HermesSocketServer.Store;
|
|
|
|
|
|
|
|
namespace HermesSocketServer.Services
|
|
|
|
{
|
|
|
|
public class DatabaseService : BackgroundService
|
|
|
|
{
|
2024-10-20 15:32:30 -04:00
|
|
|
private readonly ChannelManager _channels;
|
2024-10-17 17:17:09 -04:00
|
|
|
private readonly VoiceStore _voices;
|
2024-10-18 21:50:46 -04:00
|
|
|
private readonly UserStore _users;
|
2024-10-17 23:21:16 -04:00
|
|
|
private readonly ServerConfiguration _configuration;
|
2024-10-17 17:17:09 -04:00
|
|
|
private readonly Serilog.ILogger _logger;
|
|
|
|
|
2024-10-20 15:32:30 -04:00
|
|
|
public DatabaseService(ChannelManager channels, VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger)
|
2024-10-18 21:50:46 -04:00
|
|
|
{
|
2024-10-20 15:32:30 -04:00
|
|
|
_channels = channels;
|
2024-10-17 17:17:09 -04:00
|
|
|
_voices = voices;
|
2024-10-18 21:50:46 -04:00
|
|
|
_users = users;
|
2024-10-17 23:21:16 -04:00
|
|
|
_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-18 21:50:46 -04:00
|
|
|
_logger.Information("Loading users...");
|
|
|
|
await _users.Load();
|
2024-10-17 17:17:09 -04:00
|
|
|
|
|
|
|
await Task.Run(async () =>
|
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
2024-10-21 16:44:20 -04:00
|
|
|
|
2024-10-17 17:17:09 -04:00
|
|
|
while (true)
|
|
|
|
{
|
2024-10-20 15:32:30 -04:00
|
|
|
await Task.WhenAll([
|
|
|
|
_voices.Save(),
|
|
|
|
_users.Save(),
|
|
|
|
_channels.Save(),
|
|
|
|
]);
|
2024-10-21 16:44:20 -04:00
|
|
|
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
2024-10-17 17:17:09 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|