From 2d24e4730e0f9678c0d7833c2332c7f0821eb7e2 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Thu, 6 Aug 2020 23:48:06 +0200 Subject: Verification: persist task settings in Redis If tasks are stopped manually, they will not automatically restart on cog reload or bot restart. Using `maybe_start_tasks` is necessary because we cannot interface with Redis from a sync context. We're using 1 and 0 because RedisCache does not currently permit bool values due to a typestring conversion bug. --- bot/cogs/verification.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/bot/cogs/verification.py b/bot/cogs/verification.py index 152118d92..b4dc1f145 100644 --- a/bot/cogs/verification.py +++ b/bot/cogs/verification.py @@ -111,9 +111,7 @@ class Verification(Cog): def __init__(self, bot: Bot) -> None: """Start internal tasks.""" self.bot = bot - - self.update_unverified_members.start() - self.ping_unverified.start() + self.bot.loop.create_task(self.maybe_start_tasks()) def cog_unload(self) -> None: """ @@ -129,6 +127,20 @@ class Verification(Cog): """Get currently loaded ModLog cog instance.""" return self.bot.get_cog("ModLog") + async def maybe_start_tasks(self) -> None: + """ + Poll Redis to check whether internal tasks should start. + + Redis must be interfaced with from an async function. + """ + log.trace("Checking whether background tasks should begin") + setting: t.Optional[int] = await self.reminder_cache.get("tasks_running") # This can be None if never set + + if setting: + log.trace("Background tasks will be started") + self.update_unverified_members.start() + self.ping_unverified.start() + # region: automatically update unverified users async def _verify_kick(self, n_members: int) -> bool: @@ -492,6 +504,8 @@ class Verification(Cog): if not self.ping_unverified.is_running(): self.ping_unverified.start() + await self.reminder_cache.set("tasks_running", 1) + colour = discord.Colour.blurple() await ctx.send(embed=discord.Embed(title="Verification system", description="Done. :ok_hand:", colour=colour)) @@ -503,6 +517,8 @@ class Verification(Cog): self.update_unverified_members.cancel() self.ping_unverified.cancel() + await self.reminder_cache.set("tasks_running", 0) + colour = discord.Colour.blurple() await ctx.send(embed=discord.Embed(title="Verification system", description="Tasks canceled.", colour=colour)) -- cgit v1.2.3