hermes-server/Services/DatabaseService.cs

41 lines
1.4 KiB
C#

using HermesSocketServer.Store;
namespace HermesSocketServer.Services
{
public class DatabaseService : BackgroundService
{
private readonly VoiceStore _voices;
private readonly UserStore _users;
private readonly ServerConfiguration _configuration;
private readonly Serilog.ILogger _logger;
public DatabaseService(VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger)
{
_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 () =>
{
var tasks = new List<Task>();
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
while (true)
{
tasks.Add(_voices.Save());
tasks.Add(_users.Save());
tasks.Add(Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds)));
await Task.WhenAll(tasks);
}
});
}
}
}