mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 00:19:00 +03:00
Add periodic guild data saving (every 5 minutes) (#99)
This PR adds a BackgroundGuildDataSaverService which will save all guild data to disk every 5 minutes using a PeriodicTimer. Closes #96 Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
324f455404
commit
37ebf0ffa0
3 changed files with 32 additions and 4 deletions
|
@ -92,6 +92,7 @@ public sealed class Boyfriend
|
|||
.AddHostedService<MemberUpdateService>()
|
||||
.AddHostedService<ScheduledEventUpdateService>()
|
||||
.AddHostedService<SongUpdateService>()
|
||||
.AddHostedService<BackgroundGuildDataSaverService>()
|
||||
// Slash commands
|
||||
.AddCommandTree()
|
||||
.WithCommandGroup<AboutCommandGroup>()
|
||||
|
|
23
src/Services/BackgroundGuildDataSaverService.cs
Normal file
23
src/Services/BackgroundGuildDataSaverService.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Boyfriend.Services;
|
||||
|
||||
public sealed class BackgroundGuildDataSaverService : BackgroundService
|
||||
{
|
||||
private readonly GuildDataService _guildData;
|
||||
|
||||
public BackgroundGuildDataSaverService(GuildDataService guildData)
|
||||
{
|
||||
_guildData = guildData;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
|
||||
|
||||
while (await timer.WaitForNextTickAsync(ct))
|
||||
{
|
||||
await _guildData.SaveAsync(ct);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ using System.Text.Json;
|
|||
using System.Text.Json.Nodes;
|
||||
using Boyfriend.Data;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Remora.Discord.API.Abstractions.Rest;
|
||||
using Remora.Rest.Core;
|
||||
|
||||
|
@ -15,12 +16,14 @@ public sealed class GuildDataService : IHostedService
|
|||
{
|
||||
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
|
||||
private readonly IDiscordRestGuildAPI _guildApi;
|
||||
private readonly ILogger<GuildDataService> _logger;
|
||||
|
||||
// https://github.com/dotnet/aspnetcore/issues/39139
|
||||
public GuildDataService(
|
||||
IHostApplicationLifetime lifetime, IDiscordRestGuildAPI guildApi)
|
||||
IHostApplicationLifetime lifetime, IDiscordRestGuildAPI guildApi, ILogger<GuildDataService> logger)
|
||||
{
|
||||
_guildApi = guildApi;
|
||||
_logger = logger;
|
||||
lifetime.ApplicationStopping.Register(ApplicationStopping);
|
||||
}
|
||||
|
||||
|
@ -39,8 +42,9 @@ public sealed class GuildDataService : IHostedService
|
|||
SaveAsync(CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private async Task SaveAsync(CancellationToken ct)
|
||||
public async Task SaveAsync(CancellationToken ct)
|
||||
{
|
||||
_logger.LogInformation("Saving guild data...");
|
||||
var tasks = new List<Task>();
|
||||
foreach (var data in _datas.Values)
|
||||
{
|
||||
|
@ -89,7 +93,7 @@ public sealed class GuildDataService : IHostedService
|
|||
|
||||
await using var eventsStream = File.OpenRead(scheduledEventsPath);
|
||||
var events
|
||||
= JsonSerializer.DeserializeAsync<Dictionary<ulong, ScheduledEventData>>(
|
||||
= await JsonSerializer.DeserializeAsync<Dictionary<ulong, ScheduledEventData>>(
|
||||
eventsStream, cancellationToken: ct);
|
||||
|
||||
var memberData = new Dictionary<ulong, MemberData>();
|
||||
|
@ -113,7 +117,7 @@ public sealed class GuildDataService : IHostedService
|
|||
|
||||
var finalData = new GuildData(
|
||||
jsonSettings ?? new JsonObject(), settingsPath,
|
||||
await events ?? new Dictionary<ulong, ScheduledEventData>(), scheduledEventsPath,
|
||||
events ?? new Dictionary<ulong, ScheduledEventData>(), scheduledEventsPath,
|
||||
memberData, memberDataPath);
|
||||
|
||||
_datas.TryAdd(guildId, finalData);
|
||||
|
|
Loading…
Reference in a new issue