46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
using HermesSocketLibrary.Requests.Messages;
|
|
using HermesSocketServer.Models;
|
|
using HermesSocketServer.Store;
|
|
|
|
namespace HermesSocketServer.Services
|
|
{
|
|
public class DatabaseService : BackgroundService
|
|
{
|
|
private readonly ChannelManager _channels;
|
|
private readonly IStore<string, TTSVoice> _voices;
|
|
private readonly IStore<string, User> _users;
|
|
private readonly ServerConfiguration _configuration;
|
|
private readonly Serilog.ILogger _logger;
|
|
|
|
public DatabaseService(ChannelManager channels, IStore<string, TTSVoice> voices, IStore<string, User> 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 users...");
|
|
await _users.Load();
|
|
_logger.Information("Loading TTS voices...");
|
|
await _voices.Load();
|
|
|
|
await Task.Run(async () =>
|
|
{
|
|
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
|
|
|
while (true)
|
|
{
|
|
await _users.Save();
|
|
await _voices.Save();
|
|
await _channels.Save();
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
} |