48 lines
1.7 KiB
C#
48 lines
1.7 KiB
C#
using HermesSocketLibrary.db;
|
|
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 TTS voices...");
|
|
await _voices.Load();
|
|
_logger.Information("Loading users...");
|
|
await _users.Load();
|
|
|
|
await Task.Run(async () =>
|
|
{
|
|
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
|
|
|
while (true)
|
|
{
|
|
await Task.WhenAll([
|
|
_voices.Save(),
|
|
_users.Save(),
|
|
_channels.Save(),
|
|
]);
|
|
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
} |