diff options
| -rw-r--r-- | bot/bot.py | 17 | ||||
| -rw-r--r-- | bot/utils/__init__.py | 4 | ||||
| -rw-r--r-- | bot/utils/redis_cache.py | 2 | 
3 files changed, 20 insertions, 3 deletions
| diff --git a/bot/bot.py b/bot/bot.py index a85a22aa9..f55eec5bb 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -5,6 +5,7 @@ import warnings  from typing import Optional  import aiohttp +import aioredis  import discord  from discord.ext import commands  from sentry_sdk import push_scope @@ -28,11 +29,13 @@ class Bot(commands.Bot):          super().__init__(*args, **kwargs)          self.http_session: Optional[aiohttp.ClientSession] = None +        self.redis_session: Optional[aioredis.Redis] = None          self.api_client = api.APIClient(loop=self.loop)          self._connector = None          self._resolver = None          self._guild_available = asyncio.Event() +        self._redis_ready = asyncio.Event()          statsd_url = constants.Stats.statsd_host @@ -42,8 +45,18 @@ class Bot(commands.Bot):              # will effectively disable stats.              statsd_url = "127.0.0.1" +        asyncio.create_task(self._create_redis_session()) +          self.stats = AsyncStatsClient(self.loop, statsd_url, 8125, prefix="bot") +    async def _create_redis_session(self) -> None: +        """Create the Redis connection pool, and then open the redis event gate.""" +        self.redis_session = await aioredis.create_redis_pool( +            address=(constants.Redis.host, constants.Redis.port), +            password=constants.Redis.password, +        ) +        self._redis_ready.set() +      def add_cog(self, cog: commands.Cog) -> None:          """Adds a "cog" to the bot and logs the operation."""          super().add_cog(cog) @@ -78,6 +91,10 @@ class Bot(commands.Bot):          if self.stats._transport:              self.stats._transport.close() +        if self.redis_session: +            self.redis_session.close() +            await self.redis_session.wait_closed() +      async def login(self, *args, **kwargs) -> None:          """Re-create the connector and set up sessions before logging into Discord."""          self._recreate() diff --git a/bot/utils/__init__.py b/bot/utils/__init__.py index 5ce383bf2..c5a12d5e3 100644 --- a/bot/utils/__init__.py +++ b/bot/utils/__init__.py @@ -2,9 +2,9 @@ from abc import ABCMeta  from discord.ext.commands import CogMeta -from bot.utils.redis_dict import RedisDict +from bot.utils.redis_cache import RedisCache -__all__ = ['RedisDict', 'CogABCMeta'] +__all__ = ['RedisCache', 'CogABCMeta']  class CogABCMeta(CogMeta, ABCMeta): diff --git a/bot/utils/redis_cache.py b/bot/utils/redis_cache.py index d0a7eba4a..467f16767 100644 --- a/bot/utils/redis_cache.py +++ b/bot/utils/redis_cache.py @@ -61,7 +61,7 @@ class RedisCache:          for attribute in vars(instance).values():              if isinstance(attribute, Bot):                  self.bot = attribute -                self._redis = self.bot.redis_pool +                self._redis = self.bot.redis_session                  return self          else:              raise RuntimeError("Cannot initialize a RedisCache without a `Bot` instance.") | 
