mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 09:09:00 +03:00
Merge BackgroundGuildDataSaverService into GuildDataService (#239)
Title. idk why I didn't think of this before. Also, GuildDataService is now properly registered as an IHostedService, so it receives start & shutdown events. So this PR gets rid of the workaround that was needed for save-on-shutdown to function Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
bd4c5b26da
commit
21f200c988
3 changed files with 21 additions and 36 deletions
|
@ -24,12 +24,12 @@ namespace Octobot;
|
||||||
|
|
||||||
public sealed class Octobot
|
public sealed class Octobot
|
||||||
{
|
{
|
||||||
public static readonly AllowedMentions NoMentions = new(
|
|
||||||
Array.Empty<MentionType>(), Array.Empty<Snowflake>(), Array.Empty<Snowflake>());
|
|
||||||
|
|
||||||
public const string RepositoryUrl = "https://github.com/LabsDevelopment/Octobot";
|
public const string RepositoryUrl = "https://github.com/LabsDevelopment/Octobot";
|
||||||
public const string IssuesUrl = $"{RepositoryUrl}/issues";
|
public const string IssuesUrl = $"{RepositoryUrl}/issues";
|
||||||
|
|
||||||
|
public static readonly AllowedMentions NoMentions = new(
|
||||||
|
Array.Empty<MentionType>(), Array.Empty<Snowflake>(), Array.Empty<Snowflake>());
|
||||||
|
|
||||||
public static async Task Main(string[] args)
|
public static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
var host = CreateHostBuilder(args).UseConsoleLifetime().Build();
|
var host = CreateHostBuilder(args).UseConsoleLifetime().Build();
|
||||||
|
@ -86,12 +86,12 @@ public sealed class Octobot
|
||||||
.AddPreparationErrorEvent<LoggingPreparationErrorEvent>()
|
.AddPreparationErrorEvent<LoggingPreparationErrorEvent>()
|
||||||
.AddPostExecutionEvent<ErrorLoggingPostExecutionEvent>()
|
.AddPostExecutionEvent<ErrorLoggingPostExecutionEvent>()
|
||||||
// Services
|
// Services
|
||||||
.AddSingleton<GuildDataService>()
|
|
||||||
.AddSingleton<Utility>()
|
.AddSingleton<Utility>()
|
||||||
|
.AddSingleton<GuildDataService>()
|
||||||
|
.AddHostedService<GuildDataService>(provider => provider.GetRequiredService<GuildDataService>())
|
||||||
.AddHostedService<MemberUpdateService>()
|
.AddHostedService<MemberUpdateService>()
|
||||||
.AddHostedService<ScheduledEventUpdateService>()
|
.AddHostedService<ScheduledEventUpdateService>()
|
||||||
.AddHostedService<SongUpdateService>()
|
.AddHostedService<SongUpdateService>()
|
||||||
.AddHostedService<BackgroundGuildDataSaverService>()
|
|
||||||
// Slash commands
|
// Slash commands
|
||||||
.AddCommandTree()
|
.AddCommandTree()
|
||||||
.WithCommandGroup<AboutCommandGroup>()
|
.WithCommandGroup<AboutCommandGroup>()
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
|
|
||||||
namespace Octobot.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,25 +11,23 @@ namespace Octobot.Services;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles saving, loading, initializing and providing <see cref="GuildData" />.
|
/// Handles saving, loading, initializing and providing <see cref="GuildData" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class GuildDataService
|
public sealed class GuildDataService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
|
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
|
||||||
private readonly ILogger<GuildDataService> _logger;
|
private readonly ILogger<GuildDataService> _logger;
|
||||||
|
|
||||||
// https://github.com/dotnet/aspnetcore/issues/39139
|
public GuildDataService(ILogger<GuildDataService> logger)
|
||||||
public GuildDataService(
|
|
||||||
IHostApplicationLifetime lifetime, ILogger<GuildDataService> logger)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
lifetime.ApplicationStopping.Register(ApplicationStopping);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ApplicationStopping()
|
public override Task StopAsync(CancellationToken ct)
|
||||||
{
|
{
|
||||||
SaveAsync(CancellationToken.None).GetAwaiter().GetResult();
|
base.StopAsync(ct);
|
||||||
|
return SaveAsync(ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SaveAsync(CancellationToken ct)
|
private Task SaveAsync(CancellationToken ct)
|
||||||
{
|
{
|
||||||
var tasks = new List<Task>();
|
var tasks = new List<Task>();
|
||||||
var datas = _datas.Values.ToArray();
|
var datas = _datas.Values.ToArray();
|
||||||
|
@ -58,6 +56,16 @@ public sealed class GuildDataService
|
||||||
File.Delete(tempFilePath);
|
File.Delete(tempFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken ct)
|
||||||
|
{
|
||||||
|
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
|
||||||
|
|
||||||
|
while (await timer.WaitForNextTickAsync(ct))
|
||||||
|
{
|
||||||
|
await SaveAsync(ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<GuildData> GetData(Snowflake guildId, CancellationToken ct = default)
|
public async Task<GuildData> GetData(Snowflake guildId, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
return _datas.TryGetValue(guildId, out var data) ? data : await InitializeData(guildId, ct);
|
return _datas.TryGetValue(guildId, out var data) ? data : await InitializeData(guildId, ct);
|
||||||
|
|
Loading…
Reference in a new issue