aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-02-28 11:35:47 -0800
committerGravatar MarkKoz <[email protected]>2020-03-22 15:54:41 -0700
commitfc5bda29a0cb6beee931d48a83bc5f093a22499c (patch)
tree0a2032682c851957adeae4761546c2cf81a61769
parentHelpChannels: add trace logging for notifications (diff)
HelpChannels: cancel channel queue tasks on cog unload
* Store queue get() tasks in a list * Create a separate function to wait for a channel from the queue * Add comments for the various groups of attributes defined in __init__
-rw-r--r--bot/cogs/help_channels.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py
index d226b201a..6bed3199c 100644
--- a/bot/cogs/help_channels.py
+++ b/bot/cogs/help_channels.py
@@ -92,16 +92,20 @@ class HelpChannels(Scheduler, commands.Cog):
self.bot = bot
+ # Categories
self.available_category: discord.CategoryChannel = None
self.in_use_category: discord.CategoryChannel = None
self.dormant_category: discord.CategoryChannel = None
+ # Queues
self.channel_queue: asyncio.Queue[discord.TextChannel] = None
self.name_queue: t.Deque[str] = None
self.elements = self.get_names()
self.last_notification: t.Optional[datetime] = None
+ # Asyncio stuff
+ self.queue_tasks: t.List[asyncio.Task] = []
self.ready = asyncio.Event()
self.on_message_lock = asyncio.Lock()
self.init_task = self.bot.loop.create_task(self.init_cog())
@@ -111,6 +115,10 @@ class HelpChannels(Scheduler, commands.Cog):
log.trace("Cog unload: cancelling the cog_init task")
self.init_task.cancel()
+ log.trace("Cog unload: cancelling the channel queue tasks")
+ for task in self.queue_tasks:
+ task.cancel()
+
log.trace("Cog unload: cancelling the scheduled tasks")
for task in self.scheduled_tasks.values():
task.cancel()
@@ -194,7 +202,7 @@ class HelpChannels(Scheduler, commands.Cog):
if not channel:
log.info("Couldn't create a candidate channel; waiting to get one from the queue.")
await self.notify()
- channel = await self.channel_queue.get()
+ await self.wait_for_dormant_channel()
return channel
@@ -535,6 +543,19 @@ class HelpChannels(Scheduler, commands.Cog):
log.trace(f"Channel #{channel.name} ({channel_id}) retrieved.")
return channel
+ async def wait_for_dormant_channel(self) -> discord.TextChannel:
+ """Wait for a dormant channel to become available in the queue and return it."""
+ log.trace("Waiting for a dormant channel.")
+
+ task = asyncio.create_task(self.channel_queue.get())
+ self.queue_tasks.append(task)
+ channel = await task
+
+ log.trace(f"Channel #{channel.name} ({channel.id}) finally retrieved from the queue.")
+ self.queue_tasks.remove(task)
+
+ return channel
+
async def _scheduled_task(self, data: ChannelTimeout) -> None:
"""Make a channel dormant after specified timeout or reschedule if it's still active."""
log.trace(f"Waiting {data.timeout} seconds before making #{data.channel.name} dormant.")