BirthdayBot/BackgroundServices/AutoUserDownload.cs

101 lines
4 KiB
C#
Raw Permalink Normal View History

using BirthdayBot.Data;
using Microsoft.EntityFrameworkCore;
namespace BirthdayBot.BackgroundServices;
/// <summary>
/// Proactively fills the user cache for guilds in which any birthday data already exists.
/// </summary>
class AutoUserDownload : BackgroundService {
private static readonly TimeSpan RequestTimeout = ShardManager.DeadShardThreshold / 3;
private readonly HashSet<ulong> _skippedGuilds = [];
public AutoUserDownload(ShardInstance instance) : base(instance)
=> Shard.DiscordClient.Disconnected += OnDisconnect;
~AutoUserDownload() => Shard.DiscordClient.Disconnected -= OnDisconnect;
private Task OnDisconnect(Exception ex) {
_skippedGuilds.Clear();
return Task.CompletedTask;
}
2023-03-04 19:27:31 +00:00
public override async Task OnTick(int tickCount, CancellationToken token) {
2022-03-21 19:11:30 +00:00
// Take action if a guild's cache is incomplete...
var incompleteCaches = Shard.DiscordClient.Guilds
.Where(g => !g.HasAllMembers)
.Select(g => g.Id)
.ToHashSet();
2022-03-21 19:11:30 +00:00
// ...and if the guild contains any user data
2024-04-28 08:51:58 +00:00
HashSet<ulong> mustFetch;
try {
await ConcurrentSemaphore.WaitAsync(token);
using var db = new BotDatabaseContext();
2024-04-28 08:51:58 +00:00
mustFetch = [.. db.UserEntries.AsNoTracking()
.Where(e => incompleteCaches.Contains(e.GuildId))
.Select(e => e.GuildId)
2024-04-28 08:51:58 +00:00
.Where(e => !_skippedGuilds.Contains(e))];
} finally {
try {
ConcurrentSemaphore.Release();
} catch (ObjectDisposedException) { }
}
2024-04-28 08:51:58 +00:00
var processed = 0;
2023-03-04 19:27:31 +00:00
var processStartTime = DateTimeOffset.UtcNow;
2022-03-21 19:11:30 +00:00
foreach (var item in mustFetch) {
// Take break from processing to avoid getting killed by ShardManager
if (DateTimeOffset.UtcNow - processStartTime > RequestTimeout) break;
// We're useless if not connected
if (Shard.DiscordClient.ConnectionState != ConnectionState.Connected) break;
var guild = Shard.DiscordClient.GetGuild(item);
2022-03-21 19:11:30 +00:00
if (guild == null) continue; // A guild disappeared...?
2023-03-04 19:27:31 +00:00
2022-03-22 06:33:24 +00:00
processed++;
await Task.Delay(200, CancellationToken.None); // Delay a bit (reduces the possibility of hanging, somehow).
var dl = guild.DownloadUsersAsync();
try {
dl.Wait((int)RequestTimeout.TotalMilliseconds / 2, token);
} catch (Exception) { }
if (token.IsCancellationRequested) return; // Skip all reporting, error logging on cancellation
if (dl.IsFaulted) {
Log("Exception thrown by download task: " + dl.Exception);
2023-03-04 19:27:31 +00:00
break;
} else if (!dl.IsCompletedSuccessfully) {
Log($"Task unresponsive, will skip (ID {guild.Id}, with {guild.MemberCount} members).");
2024-04-28 08:51:58 +00:00
_skippedGuilds.Add(guild.Id);
continue;
2023-03-04 19:27:31 +00:00
}
}
2022-03-22 06:33:24 +00:00
if (processed > 10) Log($"Member list downloads handled for {processed} guilds.");
ConsiderGC(processed);
}
#region Manual garbage collection
private static readonly object _mgcTrackLock = new();
private static int _mgcProcessedSinceLast = 0;
// Downloading user information adds up memory-wise, particularly within the
// Gen 2 collection. Here we attempt to balance not calling the GC too much
// while also avoiding dying to otherwise inevitable excessive memory use.
private static void ConsiderGC(int processed) {
const int CallGcAfterProcessingAmt = 1500;
bool trigger;
lock (_mgcTrackLock) {
_mgcProcessedSinceLast += processed;
trigger = _mgcProcessedSinceLast > CallGcAfterProcessingAmt;
if (trigger) _mgcProcessedSinceLast = 0;
}
if (trigger) {
Program.Log(nameof(AutoUserDownload), "Invoking garbage collection...");
GC.Collect(2, GCCollectionMode.Forced, true, true);
Program.Log(nameof(AutoUserDownload), "Complete.");
}
}
#endregion
}