2023-07-09 18:32:14 +05:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Text.Json;
|
2023-07-18 15:25:02 +03:00
|
|
|
using System.Text.Json.Nodes;
|
2023-07-09 18:32:14 +05:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2023-08-22 12:44:05 +05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-07-09 18:32:14 +05:00
|
|
|
using Remora.Rest.Core;
|
2024-05-16 20:34:26 +05:00
|
|
|
using TeamOctolings.Octobot.Data;
|
2023-07-09 18:32:14 +05:00
|
|
|
|
2024-05-16 20:34:26 +05:00
|
|
|
namespace TeamOctolings.Octobot.Services;
|
2023-07-09 18:32:14 +05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles saving, loading, initializing and providing <see cref="GuildData" />.
|
|
|
|
/// </summary>
|
2023-12-20 22:08:56 +05:00
|
|
|
public sealed class GuildDataService : BackgroundService
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2023-07-09 18:32:14 +05:00
|
|
|
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
|
2023-08-22 12:44:05 +05:00
|
|
|
private readonly ILogger<GuildDataService> _logger;
|
2023-07-09 18:32:14 +05:00
|
|
|
|
2023-12-20 22:08:56 +05:00
|
|
|
public GuildDataService(ILogger<GuildDataService> logger)
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2023-08-22 12:44:05 +05:00
|
|
|
_logger = logger;
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|
|
|
|
|
2023-12-20 22:08:56 +05:00
|
|
|
public override Task StopAsync(CancellationToken ct)
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2023-12-20 22:08:56 +05:00
|
|
|
base.StopAsync(ct);
|
|
|
|
return SaveAsync(ct);
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|
|
|
|
|
2024-06-25 15:09:45 +05:00
|
|
|
private Task SaveAsync(CancellationToken ct = default)
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2023-07-09 18:32:14 +05:00
|
|
|
var tasks = new List<Task>();
|
2023-09-12 18:28:46 +05:00
|
|
|
var datas = _datas.Values.ToArray();
|
2023-10-26 20:14:27 +05:00
|
|
|
foreach (var data in datas.Where(data => !data.DataLoadFailed))
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2023-10-26 20:14:27 +05:00
|
|
|
tasks.Add(SerializeObjectSafelyAsync(data.Settings, data.SettingsPath, ct));
|
|
|
|
tasks.Add(SerializeObjectSafelyAsync(data.ScheduledEvents, data.ScheduledEventsPath, ct));
|
2023-07-09 18:32:14 +05:00
|
|
|
|
2023-09-12 18:28:46 +05:00
|
|
|
var memberDatas = data.MemberData.Values.ToArray();
|
2023-10-26 20:14:27 +05:00
|
|
|
tasks.AddRange(memberDatas.Select(memberData =>
|
|
|
|
SerializeObjectSafelyAsync(memberData, $"{data.MemberDataPath}/{memberData.Id}.json", ct)));
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|
|
|
|
|
2023-12-06 00:24:55 +05:00
|
|
|
return Task.WhenAll(tasks);
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|
|
|
|
|
2024-06-25 15:09:45 +05:00
|
|
|
private static async Task SerializeObjectSafelyAsync<T>(T obj, string path, CancellationToken ct = default)
|
2023-10-26 20:14:27 +05:00
|
|
|
{
|
|
|
|
var tempFilePath = path + ".tmp";
|
|
|
|
await using (var tempFileStream = File.Create(tempFilePath))
|
|
|
|
{
|
|
|
|
await JsonSerializer.SerializeAsync(tempFileStream, obj, cancellationToken: ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
File.Copy(tempFilePath, path, true);
|
|
|
|
File.Delete(tempFilePath);
|
|
|
|
}
|
|
|
|
|
2023-12-20 22:08:56 +05:00
|
|
|
protected override async Task ExecuteAsync(CancellationToken ct)
|
|
|
|
{
|
|
|
|
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
|
|
|
|
|
|
|
|
while (await timer.WaitForNextTickAsync(ct))
|
|
|
|
{
|
|
|
|
await SaveAsync(ct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 01:51:16 +05:00
|
|
|
public async Task<GuildData> GetData(Snowflake guildId, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 18:32:14 +05:00
|
|
|
return _datas.TryGetValue(guildId, out var data) ? data : await InitializeData(guildId, ct);
|
|
|
|
}
|
|
|
|
|
2023-08-03 01:51:16 +05:00
|
|
|
private async Task<GuildData> InitializeData(Snowflake guildId, CancellationToken ct = default)
|
|
|
|
{
|
2023-09-22 15:33:14 +03:00
|
|
|
var path = $"GuildData/{guildId}";
|
|
|
|
var memberDataPath = $"{path}/MemberData";
|
2025-02-03 16:58:57 +05:00
|
|
|
|
2023-09-22 15:33:14 +03:00
|
|
|
var settingsPath = $"{path}/Settings.json";
|
2025-02-03 16:58:57 +05:00
|
|
|
|
2023-09-22 15:33:14 +03:00
|
|
|
var scheduledEventsPath = $"{path}/ScheduledEvents.json";
|
|
|
|
|
2024-04-01 22:20:41 +03:00
|
|
|
MigrateDataDirectory(guildId, path);
|
2023-09-22 15:33:14 +03:00
|
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
2023-08-03 01:51:16 +05:00
|
|
|
|
2025-02-03 16:58:57 +05:00
|
|
|
var dataLoadFailed = false;
|
|
|
|
|
|
|
|
var jsonSettings = await LoadGuildSettings(settingsPath, ct);
|
|
|
|
if (jsonSettings is not null)
|
|
|
|
{
|
|
|
|
FixJsonSettings(jsonSettings);
|
|
|
|
}
|
|
|
|
else
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
dataLoadFailed = true;
|
2023-08-03 01:51:16 +05:00
|
|
|
}
|
|
|
|
|
2025-02-03 16:58:57 +05:00
|
|
|
var events = await LoadScheduledEvents(scheduledEventsPath, ct);
|
|
|
|
if (events is null)
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
dataLoadFailed = true;
|
2023-08-03 01:51:16 +05:00
|
|
|
}
|
2023-07-09 18:32:14 +05:00
|
|
|
|
2025-02-03 16:58:57 +05:00
|
|
|
var memberData = new Dictionary<ulong, MemberData>();
|
|
|
|
foreach (var dataFileInfo in Directory.CreateDirectory(memberDataPath).GetFiles()
|
|
|
|
.Where(dataFileInfo =>
|
|
|
|
!memberData.ContainsKey(
|
|
|
|
ulong.Parse(dataFileInfo.Name.Replace(".json", "").Replace(".tmp", "")))))
|
|
|
|
{
|
|
|
|
var data = await LoadMemberData(dataFileInfo, memberDataPath, true, ct);
|
|
|
|
|
|
|
|
if (data == null)
|
|
|
|
{
|
|
|
|
dataLoadFailed = true;
|
|
|
|
continue;
|
|
|
|
}
|
2023-10-26 20:14:27 +05:00
|
|
|
|
2025-02-03 16:58:57 +05:00
|
|
|
memberData.TryAdd(data.Id, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
var finalData = new GuildData(
|
|
|
|
jsonSettings ?? new JsonObject(), settingsPath,
|
|
|
|
events ?? new Dictionary<ulong, ScheduledEventData>(), scheduledEventsPath,
|
|
|
|
memberData, memberDataPath,
|
|
|
|
dataLoadFailed);
|
|
|
|
|
|
|
|
_datas.TryAdd(guildId, finalData);
|
|
|
|
|
|
|
|
return finalData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<MemberData?> LoadMemberData(FileInfo dataFileInfo, string memberDataPath, bool loadTmp,
|
|
|
|
CancellationToken ct = default)
|
|
|
|
{
|
|
|
|
MemberData? data;
|
|
|
|
var temporaryPath = $"{dataFileInfo.FullName}.tmp";
|
|
|
|
var usedInfo = loadTmp && File.Exists(temporaryPath) ? new FileInfo(temporaryPath) : dataFileInfo;
|
|
|
|
|
|
|
|
var isTmp = usedInfo.Extension is ".tmp";
|
2023-10-26 20:14:27 +05:00
|
|
|
try
|
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
await using var dataStream = usedInfo.OpenRead();
|
|
|
|
data = await JsonSerializer.DeserializeAsync<MemberData>(dataStream, cancellationToken: ct);
|
|
|
|
if (isTmp)
|
|
|
|
{
|
|
|
|
usedInfo.CopyTo(usedInfo.FullName.Replace(".tmp", ""), true);
|
|
|
|
usedInfo.Delete();
|
|
|
|
}
|
2023-10-26 20:14:27 +05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
if (isTmp)
|
|
|
|
{
|
|
|
|
_logger.LogWarning(e,
|
|
|
|
"Unable to load temporary member data file, deleting: {MemberDataPath}/{FileName}", memberDataPath,
|
|
|
|
usedInfo.Name);
|
|
|
|
usedInfo.Delete();
|
|
|
|
return await LoadMemberData(dataFileInfo, memberDataPath, false, ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogError(e, "Member data load failed: {MemberDataPath}/{FileName}", memberDataPath,
|
|
|
|
usedInfo.Name);
|
|
|
|
return null;
|
2023-10-26 20:14:27 +05:00
|
|
|
}
|
2023-07-09 18:32:14 +05:00
|
|
|
|
2025-02-03 16:58:57 +05:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Dictionary<ulong, ScheduledEventData>?> LoadScheduledEvents(string scheduledEventsPath,
|
|
|
|
CancellationToken ct = default)
|
|
|
|
{
|
|
|
|
var tempScheduledEventsPath = $"{scheduledEventsPath}.tmp";
|
|
|
|
|
|
|
|
if (!File.Exists(scheduledEventsPath) && !File.Exists(tempScheduledEventsPath))
|
2024-04-01 22:20:41 +03:00
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
return new Dictionary<ulong, ScheduledEventData>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (File.Exists(tempScheduledEventsPath))
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Found temporary scheduled events file, will try to parse and copy to main: ${Path}",
|
|
|
|
tempScheduledEventsPath);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await using var tempEventsStream = File.OpenRead(tempScheduledEventsPath);
|
|
|
|
var events = await JsonSerializer.DeserializeAsync<Dictionary<ulong, ScheduledEventData>>(
|
|
|
|
tempEventsStream, cancellationToken: ct);
|
|
|
|
File.Copy(tempScheduledEventsPath, scheduledEventsPath, true);
|
|
|
|
File.Delete(tempScheduledEventsPath);
|
|
|
|
|
|
|
|
_logger.LogInformation("Successfully loaded temporary scheduled events file: ${Path}",
|
|
|
|
tempScheduledEventsPath);
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.LogError(e, "Unable to load temporary scheduled events file: {Path}, deleting",
|
|
|
|
tempScheduledEventsPath);
|
|
|
|
File.Delete(tempScheduledEventsPath);
|
|
|
|
}
|
2024-04-01 22:20:41 +03:00
|
|
|
}
|
|
|
|
|
2023-10-26 20:14:27 +05:00
|
|
|
try
|
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
await using var eventsStream = File.OpenRead(scheduledEventsPath);
|
|
|
|
return await JsonSerializer.DeserializeAsync<Dictionary<ulong, ScheduledEventData>>(
|
2023-07-09 18:32:14 +05:00
|
|
|
eventsStream, cancellationToken: ct);
|
2023-10-26 20:14:27 +05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.LogError(e, "Guild scheduled events load failed: {Path}", scheduledEventsPath);
|
2025-02-03 16:58:57 +05:00
|
|
|
return null;
|
2023-10-26 20:14:27 +05:00
|
|
|
}
|
2025-02-03 16:58:57 +05:00
|
|
|
}
|
2023-07-09 18:32:14 +05:00
|
|
|
|
2025-02-03 16:58:57 +05:00
|
|
|
private async Task<JsonNode?> LoadGuildSettings(string settingsPath, CancellationToken ct = default)
|
|
|
|
{
|
|
|
|
var tempSettingsPath = $"{settingsPath}.tmp";
|
|
|
|
|
|
|
|
if (!File.Exists(settingsPath) && !File.Exists(tempSettingsPath))
|
2023-08-03 01:51:16 +05:00
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
return new JsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (File.Exists(tempSettingsPath))
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Found temporary settings file, will try to parse and copy to main: ${Path}",
|
|
|
|
tempSettingsPath);
|
2023-10-26 20:14:27 +05:00
|
|
|
try
|
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
await using var tempSettingsStream = File.OpenRead(tempSettingsPath);
|
|
|
|
var jsonSettings = await JsonNode.ParseAsync(tempSettingsStream, cancellationToken: ct);
|
|
|
|
|
|
|
|
File.Copy(tempSettingsPath, settingsPath, true);
|
|
|
|
File.Delete(tempSettingsPath);
|
|
|
|
|
|
|
|
_logger.LogInformation("Successfully loaded temporary settings file: ${Path}", tempSettingsPath);
|
|
|
|
return jsonSettings;
|
2023-10-26 20:14:27 +05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2025-02-03 16:58:57 +05:00
|
|
|
_logger.LogError(e, "Unable to load temporary settings file: {Path}, deleting", tempSettingsPath);
|
|
|
|
File.Delete(tempSettingsPath);
|
2023-08-03 01:51:16 +05:00
|
|
|
}
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|
|
|
|
|
2025-02-03 16:58:57 +05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await using var settingsStream = File.OpenRead(settingsPath);
|
|
|
|
return await JsonNode.ParseAsync(settingsStream, cancellationToken: ct);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.LogError(e, "Guild settings load failed: {Path}", settingsPath);
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|
|
|
|
|
2024-04-01 22:20:41 +03:00
|
|
|
private void MigrateDataDirectory(Snowflake guildId, string newPath)
|
2023-09-22 15:33:14 +03:00
|
|
|
{
|
|
|
|
var oldPath = $"{guildId}";
|
|
|
|
|
|
|
|
if (Directory.Exists(oldPath))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory($"{newPath}/..");
|
|
|
|
Directory.Move(oldPath, newPath);
|
|
|
|
|
2023-10-26 20:14:27 +05:00
|
|
|
_logger.LogInformation("Moved guild data to separate folder: \"{OldPath}\" -> \"{NewPath}\"", oldPath,
|
|
|
|
newPath);
|
2023-09-22 15:33:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 22:20:41 +03:00
|
|
|
private static void FixJsonSettings(JsonNode settings)
|
|
|
|
{
|
|
|
|
var language = settings[GuildSettings.Language.Name]?.GetValue<string>();
|
|
|
|
if (language is "mctaylors-ru")
|
|
|
|
{
|
|
|
|
settings[GuildSettings.Language.Name] = "ru";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 01:51:16 +05:00
|
|
|
public async Task<JsonNode> GetSettings(Snowflake guildId, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return (await GetData(guildId, ct)).Settings;
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|
|
|
|
|
2023-08-03 01:51:16 +05:00
|
|
|
public ICollection<Snowflake> GetGuildIds()
|
|
|
|
{
|
2023-07-09 18:32:14 +05:00
|
|
|
return _datas.Keys;
|
|
|
|
}
|
2023-10-04 15:58:56 +03:00
|
|
|
|
|
|
|
public bool UnloadGuildData(Snowflake id)
|
|
|
|
{
|
|
|
|
return _datas.TryRemove(id, out _);
|
|
|
|
}
|
2023-07-09 18:32:14 +05:00
|
|
|
}
|