From a96f06d89e35336e2a40c09506ca3edc950342ba Mon Sep 17 00:00:00 2001 From: Twinki Date: Sun, 22 Sep 2024 09:49:54 -0400 Subject: [PATCH] Upgrade packages & catch exceptions in GuildEventMonitorService loop (#75) # Motivations GuildEventMonitorService keeps failing when it encounters a total request drop from either Discord or the client # Modifications - Upgrade all packages to latest - Add a `try` `catch` to `GuildEventMonitorService` in hopes of assuring the entire background service doesn't fail at random whenever packets get dropped --- src/Miha.Discord/Miha.Discord.csproj | 6 ++-- .../Hosted/GuildEventMonitorService.cs | 35 ++++++++++++------- src/Miha.Logic/Miha.Logic.csproj | 2 +- src/Miha.Redis/Miha.Redis.csproj | 2 +- src/Miha.Shared/Miha.Shared.csproj | 6 ++-- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/Miha.Discord/Miha.Discord.csproj b/src/Miha.Discord/Miha.Discord.csproj index f89e1f6..d4a29cd 100644 --- a/src/Miha.Discord/Miha.Discord.csproj +++ b/src/Miha.Discord/Miha.Discord.csproj @@ -10,10 +10,10 @@ - + - - + + diff --git a/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs b/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs index 4f29708..7e719f1 100644 --- a/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs +++ b/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs @@ -36,28 +36,37 @@ public partial class GuildEventMonitorService( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Waiting for client to be ready..."); - + await Client.WaitForReadyAsync(stoppingToken); while (!stoppingToken.IsCancellationRequested) { - await CheckScheduledEventsAsync(); + try + { + await CheckScheduledEventsAsync(); + + var utcNow = easternStandardZonedClock.GetCurrentInstant().ToDateTimeUtc(); + var nextUtc = _cron.GetNextOccurrence(DateTimeOffset.UtcNow, easternStandardZonedClock.GetTimeZoneInfo()); + + if (nextUtc is null) + { + _logger.LogWarning("Next utc occurence is null"); + await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); + continue; + } + + var next = nextUtc.Value - utcNow; - var utcNow = easternStandardZonedClock.GetCurrentInstant().ToDateTimeUtc(); - var nextUtc = _cron.GetNextOccurrence(DateTimeOffset.UtcNow, easternStandardZonedClock.GetTimeZoneInfo()); + _logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3)); - if (nextUtc is null) + await Task.Delay(nextUtc.Value - utcNow, stoppingToken); + } + catch (Exception e) { - _logger.LogWarning("Next utc occurence is null"); + _logger.LogError(e, "Exception encountered in GuildEventMonitorService"); + await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); - continue; } - - var next = nextUtc.Value - utcNow; - - _logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3)); - - await Task.Delay(nextUtc.Value - utcNow, stoppingToken); } } diff --git a/src/Miha.Logic/Miha.Logic.csproj b/src/Miha.Logic/Miha.Logic.csproj index e360038..7c538a2 100644 --- a/src/Miha.Logic/Miha.Logic.csproj +++ b/src/Miha.Logic/Miha.Logic.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/Miha.Redis/Miha.Redis.csproj b/src/Miha.Redis/Miha.Redis.csproj index 67b4255..d471518 100644 --- a/src/Miha.Redis/Miha.Redis.csproj +++ b/src/Miha.Redis/Miha.Redis.csproj @@ -10,7 +10,7 @@ - + diff --git a/src/Miha.Shared/Miha.Shared.csproj b/src/Miha.Shared/Miha.Shared.csproj index e436505..a006c4c 100644 --- a/src/Miha.Shared/Miha.Shared.csproj +++ b/src/Miha.Shared/Miha.Shared.csproj @@ -17,13 +17,13 @@ - + - + - +