Add logging for long-running task

This commit is contained in:
Noi 2022-07-27 22:00:10 -07:00
parent ebdaa6482c
commit bbe0a84532
2 changed files with 8 additions and 4 deletions

View file

@ -10,7 +10,7 @@ class EntityCacheService : Service {
internal EntityCacheService(RegexbotClient bot) : base(bot) {
// Currently we only have UserCache. May add Channel and Server caches later.
_uc = new UserCachingSubservice(bot);
_uc = new UserCachingSubservice(bot, Log);
_mc = new MessageCachingSubservice(bot, Log);
}

View file

@ -10,19 +10,23 @@ namespace RegexBot.Services.EntityCache;
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")]
class UserCachingSubservice {
internal UserCachingSubservice(RegexbotClient bot) {
private readonly Action<string> _log;
internal UserCachingSubservice(RegexbotClient bot, Action<string> logMethod) {
_log = logMethod;
bot.DiscordClient.GuildMembersDownloaded += DiscordClient_GuildMembersDownloaded;
bot.DiscordClient.GuildMemberUpdated += DiscordClient_GuildMemberUpdated;
bot.DiscordClient.UserUpdated += DiscordClient_UserUpdated;
}
private async Task DiscordClient_GuildMembersDownloaded(SocketGuild arg) {
_log($"{arg.Name}: Full member list obtained. Cache update in progress.");
using var db = new BotDatabaseContext();
foreach (var user in arg.Users) {
UpdateUser(user, db);
UpdateGuildUser(user, db);
}
await db.SaveChangesAsync();
var changes = await db.SaveChangesAsync();
_log($"{arg.Name}: Member caches updated ({changes} database writes).");
}
private async Task DiscordClient_GuildMemberUpdated(Discord.Cacheable<SocketGuildUser, ulong> old, SocketGuildUser current) {