Skip to content

Commit

Permalink
Upgrade packages & catch exceptions in GuildEventMonitorService loop (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Twinki14 authored Sep 22, 2024
1 parent 79631f5 commit a96f06d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/Miha.Discord/Miha.Discord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<ItemGroup>
<PackageReference Include="Cronos" Version="0.8.4" />
<PackageReference Include="Discord.Addons.Hosting" Version="6.1.0" />
<PackageReference Include="Discord.Net" Version="3.15.2" />
<PackageReference Include="Discord.Net" Version="3.16.0" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="SlimMessageBus" Version="2.0.2" />
<PackageReference Include="SlimMessageBus.Host.Memory" Version="2.3.5" />
<PackageReference Include="SlimMessageBus" Version="2.0.4" />
<PackageReference Include="SlimMessageBus.Host.Memory" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 22 additions & 13 deletions src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Miha.Logic/Miha.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Discord.Addons.Hosting" Version="6.1.0" />
<PackageReference Include="Discord.Net" Version="3.15.2" />
<PackageReference Include="Discord.Net" Version="3.16.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Miha.Redis/Miha.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="FluentResults" Version="3.16.0" />
<PackageReference Include="FluentResults.Extensions.FluentAssertions" Version="2.1.2" />
<PackageReference Include="Redis.OM" Version="0.7.1" />
<PackageReference Include="Redis.OM" Version="0.7.4" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Miha.Shared/Miha.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="NodaTime" Version="3.1.11" />
<PackageReference Include="NodaTime" Version="3.1.12" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.2.0" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="TimeZoneConverter" Version="6.1.0" />
</ItemGroup>
Expand Down

0 comments on commit a96f06d

Please sign in to comment.