Added channel saving. Fixed channel loading. Added policy store to channels.
This commit is contained in:
@@ -11,6 +11,7 @@ namespace HermesSocketServer.Services
|
||||
private readonly Database _database;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
private readonly IDictionary<string, Channel> _channels;
|
||||
private readonly object _lock;
|
||||
|
||||
public ChannelManager(UserStore users, Database database, Serilog.ILogger logger)
|
||||
{
|
||||
@@ -18,32 +19,45 @@ namespace HermesSocketServer.Services
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
_channels = new ConcurrentDictionary<string, Channel>();
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
|
||||
public async Task Add(string userId)
|
||||
public async Task<Channel?> Add(string userId)
|
||||
{
|
||||
var user = _users.Get(userId);
|
||||
if (user == null)
|
||||
{
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
if (_channels.ContainsKey(userId))
|
||||
lock (_lock)
|
||||
{
|
||||
return;
|
||||
if (_channels.ContainsKey(userId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var chatters = new ChatterStore(userId, _database, _logger);
|
||||
await chatters.Load();
|
||||
var policies = new PolicyStore(userId, _database, _logger);
|
||||
await Task.WhenAll([
|
||||
chatters.Load(),
|
||||
policies.Load(),
|
||||
]);
|
||||
|
||||
var channel = new Channel()
|
||||
{
|
||||
Id = userId,
|
||||
User = user,
|
||||
Chatters = chatters
|
||||
Chatters = chatters,
|
||||
Policies = policies
|
||||
};
|
||||
|
||||
_channels.Add(userId, channel);
|
||||
lock (_lock)
|
||||
{
|
||||
_channels.Add(userId, channel);
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
public Channel? Get(string channelId)
|
||||
@@ -52,5 +66,28 @@ namespace HermesSocketServer.Services
|
||||
return channel;
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task Save(string userId)
|
||||
{
|
||||
if (!_channels.TryGetValue(userId, out var channel))
|
||||
return;
|
||||
|
||||
await Task.WhenAll([
|
||||
channel.Chatters.Save(),
|
||||
channel.Policies.Save(),
|
||||
]);
|
||||
}
|
||||
|
||||
public async Task Save()
|
||||
{
|
||||
foreach (var channel in _channels.Values)
|
||||
{
|
||||
_logger.Debug($"Saving channel data to database [channel id: {channel.Id}][channel name: {channel.User.Name}]");
|
||||
await Task.WhenAll([
|
||||
channel.Chatters.Save(),
|
||||
channel.Policies.Save(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,15 @@ namespace HermesSocketServer.Services
|
||||
{
|
||||
public class DatabaseService : BackgroundService
|
||||
{
|
||||
private readonly ChannelManager _channels;
|
||||
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)
|
||||
public DatabaseService(ChannelManager channels, VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger)
|
||||
{
|
||||
_channels = channels;
|
||||
_voices = voices;
|
||||
_users = users;
|
||||
_configuration = configuration;
|
||||
@@ -26,14 +28,16 @@ namespace HermesSocketServer.Services
|
||||
|
||||
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);
|
||||
await Task.WhenAll([
|
||||
_voices.Save(),
|
||||
_users.Save(),
|
||||
_channels.Save(),
|
||||
Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds)),
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user