From 6bb0dba2230e9b3fcab2caaf559b749543c26abb Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 5 Sep 2021 09:34:54 +0100 Subject: Declare and refresh TalentPool.cache on init of cog This avoids issues in the server cog trying to access it before it's assigned and refreshed. I also migrated to the tasks to `scheduling.create_task()` as the created tasks currently don't have any error handling they can hide errors in development until the task object is destroyed (if that occurs at all) which logs the exception. The scheduling alternative attaches a callback which logs exceptions to prevent this. --- bot/exts/info/information.py | 3 ++- bot/exts/recruitment/talentpool/_cog.py | 9 ++++++--- bot/exts/recruitment/talentpool/_review.py | 2 -- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bot/exts/info/information.py b/bot/exts/info/information.py index 51d47b75c..d44886969 100644 --- a/bot/exts/info/information.py +++ b/bot/exts/info/information.py @@ -72,7 +72,8 @@ class Information(Cog): """Return additional server info only visible in moderation channels.""" talentpool_info = "" if cog := self.bot.get_cog("Talentpool"): - talentpool_info = f"Nominated: {len(cog.cache)}\n" + num_nominated = len(cog.cache) if cog.cache else 0 + talentpool_info = f"Nominated: {num_nominated}\n" bb_info = "" if cog := self.bot.get_cog("Big Brother"): diff --git a/bot/exts/recruitment/talentpool/_cog.py b/bot/exts/recruitment/talentpool/_cog.py index a317c6645..bea5ff72c 100644 --- a/bot/exts/recruitment/talentpool/_cog.py +++ b/bot/exts/recruitment/talentpool/_cog.py @@ -2,7 +2,7 @@ import logging import textwrap from collections import ChainMap, defaultdict from io import StringIO -from typing import Union +from typing import Optional, Union import discord from async_rediscache import RedisCache @@ -15,7 +15,7 @@ from bot.constants import Channels, Emojis, Guild, MODERATION_ROLES, Roles, STAF from bot.converters import MemberOrUser from bot.exts.recruitment.talentpool._review import Reviewer from bot.pagination import LinePaginator -from bot.utils import time +from bot.utils import scheduling, time from bot.utils.time import get_time_delta AUTOREVIEW_ENABLED_KEY = "autoreview_enabled" @@ -34,8 +34,11 @@ class TalentPool(Cog, name="Talentpool"): def __init__(self, bot: Bot) -> None: self.bot = bot self.reviewer = Reviewer(self.__class__.__name__, bot, self) + self.cache: Optional[defaultdict[dict]] = None self.api_default_params = {'active': 'true', 'ordering': '-inserted_at'} - self.bot.loop.create_task(self.schedule_autoreviews()) + + scheduling.create_task(self.refresh_cache(), event_loop=self.bot.loop) + scheduling.create_task(self.schedule_autoreviews(), event_loop=self.bot.loop) async def schedule_autoreviews(self) -> None: """Reschedule reviews for active nominations if autoreview is enabled.""" diff --git a/bot/exts/recruitment/talentpool/_review.py b/bot/exts/recruitment/talentpool/_review.py index 3ffbf93f3..f4aa73e75 100644 --- a/bot/exts/recruitment/talentpool/_review.py +++ b/bot/exts/recruitment/talentpool/_review.py @@ -57,8 +57,6 @@ class Reviewer: """Reschedule all active nominations to be reviewed at the appropriate time.""" log.trace("Rescheduling reviews") await self.bot.wait_until_guild_available() - # TODO Once the watch channel is removed, this can be done in a smarter way, e.g create a sync function. - await self._pool.refresh_cache() for user_id, user_data in self._pool.cache.items(): if not user_data["reviewed"]: -- cgit v1.2.3 From b412451d38a419c2b66815729cbc724e6fea0ab6 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Mon, 6 Sep 2021 13:08:04 +0100 Subject: Wait until login before trying to use the bot api client --- bot/exts/recruitment/talentpool/_cog.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/exts/recruitment/talentpool/_cog.py b/bot/exts/recruitment/talentpool/_cog.py index bea5ff72c..01a2f20e2 100644 --- a/bot/exts/recruitment/talentpool/_cog.py +++ b/bot/exts/recruitment/talentpool/_cog.py @@ -37,12 +37,14 @@ class TalentPool(Cog, name="Talentpool"): self.cache: Optional[defaultdict[dict]] = None self.api_default_params = {'active': 'true', 'ordering': '-inserted_at'} - scheduling.create_task(self.refresh_cache(), event_loop=self.bot.loop) + self.initial_refresh_task = scheduling.create_task(self.refresh_cache(), event_loop=self.bot.loop) scheduling.create_task(self.schedule_autoreviews(), event_loop=self.bot.loop) async def schedule_autoreviews(self) -> None: """Reschedule reviews for active nominations if autoreview is enabled.""" if await self.autoreview_enabled(): + # Wait for a populated cache first + await self.initial_refresh_task await self.reviewer.reschedule_reviews() else: log.trace("Not scheduling reviews as autoreview is disabled.") @@ -53,6 +55,8 @@ class TalentPool(Cog, name="Talentpool"): async def refresh_cache(self) -> bool: """Updates TalentPool users cache.""" + # Wait until logged in to ensure bot api client exists + await self.bot.wait_until_guild_available() try: data = await self.bot.api_client.get( 'bot/nominations', -- cgit v1.2.3 From 2a2b0cd34807b9a0d5292129c81d08b64ce476be Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 7 Sep 2021 12:30:46 +0100 Subject: Remove previous nominations output This raised questions from helpers when they saw that someone had many previous nominations. There is no reason why a helper needs to see this information. --- bot/exts/recruitment/talentpool/_cog.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/bot/exts/recruitment/talentpool/_cog.py b/bot/exts/recruitment/talentpool/_cog.py index 01a2f20e2..aaafff973 100644 --- a/bot/exts/recruitment/talentpool/_cog.py +++ b/bot/exts/recruitment/talentpool/_cog.py @@ -291,18 +291,7 @@ class TalentPool(Cog, name="Talentpool"): if await self.autoreview_enabled() and user.id not in self.reviewer: self.reviewer.schedule_review(user.id) - history = await self.bot.api_client.get( - 'bot/nominations', - params={ - "user__id": str(user.id), - "active": "false", - "ordering": "-inserted_at" - } - ) - msg = f"✅ The nomination for {user.mention} has been added to the talent pool" - if history: - msg += f"\n\n({len(history)} previous nominations in total)" await ctx.send(msg) -- cgit v1.2.3 From f699c56d8321004adee51df00b5105a4d8b1d3b7 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Wed, 8 Sep 2021 15:20:54 +0100 Subject: Use - rather than 0 for number of nominees when cache isn't ready --- bot/exts/info/information.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/info/information.py b/bot/exts/info/information.py index d44886969..be67910a6 100644 --- a/bot/exts/info/information.py +++ b/bot/exts/info/information.py @@ -72,7 +72,7 @@ class Information(Cog): """Return additional server info only visible in moderation channels.""" talentpool_info = "" if cog := self.bot.get_cog("Talentpool"): - num_nominated = len(cog.cache) if cog.cache else 0 + num_nominated = len(cog.cache) if cog.cache else "-" talentpool_info = f"Nominated: {num_nominated}\n" bb_info = "" -- cgit v1.2.3