From 21f200c9884ca0fc9d4129fdc5c364bc0874547c Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Wed, 20 Dec 2023 22:08:56 +0500 Subject: [PATCH] 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 --- src/Octobot.cs | 10 ++++---- .../BackgroundGuildDataSaverService.cs | 23 ------------------ src/Services/GuildDataService.cs | 24 ++++++++++++------- 3 files changed, 21 insertions(+), 36 deletions(-) delete mode 100644 src/Services/BackgroundGuildDataSaverService.cs diff --git a/src/Octobot.cs b/src/Octobot.cs index 5cffd70..2648338 100644 --- a/src/Octobot.cs +++ b/src/Octobot.cs @@ -24,12 +24,12 @@ namespace Octobot; public sealed class Octobot { - public static readonly AllowedMentions NoMentions = new( - Array.Empty(), Array.Empty(), Array.Empty()); - public const string RepositoryUrl = "https://github.com/LabsDevelopment/Octobot"; public const string IssuesUrl = $"{RepositoryUrl}/issues"; + public static readonly AllowedMentions NoMentions = new( + Array.Empty(), Array.Empty(), Array.Empty()); + public static async Task Main(string[] args) { var host = CreateHostBuilder(args).UseConsoleLifetime().Build(); @@ -86,12 +86,12 @@ public sealed class Octobot .AddPreparationErrorEvent() .AddPostExecutionEvent() // Services - .AddSingleton() .AddSingleton() + .AddSingleton() + .AddHostedService(provider => provider.GetRequiredService()) .AddHostedService() .AddHostedService() .AddHostedService() - .AddHostedService() // Slash commands .AddCommandTree() .WithCommandGroup() diff --git a/src/Services/BackgroundGuildDataSaverService.cs b/src/Services/BackgroundGuildDataSaverService.cs deleted file mode 100644 index 766ffe0..0000000 --- a/src/Services/BackgroundGuildDataSaverService.cs +++ /dev/null @@ -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); - } - } -} diff --git a/src/Services/GuildDataService.cs b/src/Services/GuildDataService.cs index 961c8f9..c9458a0 100644 --- a/src/Services/GuildDataService.cs +++ b/src/Services/GuildDataService.cs @@ -11,25 +11,23 @@ namespace Octobot.Services; /// /// Handles saving, loading, initializing and providing . /// -public sealed class GuildDataService +public sealed class GuildDataService : BackgroundService { private readonly ConcurrentDictionary _datas = new(); private readonly ILogger _logger; - // https://github.com/dotnet/aspnetcore/issues/39139 - public GuildDataService( - IHostApplicationLifetime lifetime, ILogger logger) + public GuildDataService(ILogger 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(); var datas = _datas.Values.ToArray(); @@ -58,6 +56,16 @@ public sealed class GuildDataService 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 GetData(Snowflake guildId, CancellationToken ct = default) { return _datas.TryGetValue(guildId, out var data) ? data : await InitializeData(guildId, ct);