From 2335970a790a73544565ae0472403fbac8271fe5 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 5 Apr 2022 16:08:48 +0100 Subject: Move the creation of BotBase._guild_available to within the setup hook This is to avoid a deprecation notice. --- botcore/_bot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'botcore/_bot.py') diff --git a/botcore/_bot.py b/botcore/_bot.py index 6486670d..f9e0b9fb 100644 --- a/botcore/_bot.py +++ b/botcore/_bot.py @@ -74,7 +74,7 @@ class BotBase(commands.Bot): self.statsd_url: Optional[str] = None self._statsd_timerhandle: Optional[asyncio.TimerHandle] = None - self._guild_available = asyncio.Event() + self._guild_available: Optional[asyncio.Event] = None self.stats: Optional[AsyncStatsClient] = None @@ -209,6 +209,8 @@ class BotBase(commands.Bot): """ loop = asyncio.get_running_loop() + self._guild_available = asyncio.Event() + self._resolver = aiohttp.AsyncResolver() self._connector = aiohttp.TCPConnector( resolver=self._resolver, -- cgit v1.2.3 From 863e54f989d1c73a9c94113495dfa1404cff8b07 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 5 Apr 2022 16:09:31 +0100 Subject: Create a dummy AsyncstatsdClient before connecting to the real url This is in case a connection cannot be made on init. --- botcore/_bot.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'botcore/_bot.py') diff --git a/botcore/_bot.py b/botcore/_bot.py index f9e0b9fb..e69b056d 100644 --- a/botcore/_bot.py +++ b/botcore/_bot.py @@ -89,7 +89,11 @@ class BotBase(commands.Bot): ) -> None: """Callback used to retry a connection to statsd if it should fail.""" if attempt >= 8: - log.error("Reached 8 attempts trying to reconnect AsyncStatsClient. Aborting") + log.error( + "Reached 8 attempts trying to reconnect AsyncStatsClient to %s. " + "Aborting and leaving the dummy statsd client in place.", + statsd_url, + ) return try: @@ -218,8 +222,9 @@ class BotBase(commands.Bot): ) self.http.connector = self._connector - self._connect_statsd(self.statsd_url, loop) + # Create dummy stats client first, in case `statsd_url` is unreachable within `_connect_statsd()` self.stats = AsyncStatsClient(loop, "127.0.0.1") + self._connect_statsd(self.statsd_url, loop) await self.stats.create_socket() try: -- cgit v1.2.3 From aca37f50e11f261a7d914e5b5471b2717b51e5c8 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Mon, 18 Apr 2022 15:44:22 +0100 Subject: Reconnect to redis session on setup if it is closed --- CHANGELOG.md | 3 +++ botcore/_bot.py | 7 ++++++- pyproject.toml | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'botcore/_bot.py') diff --git a/CHANGELOG.md b/CHANGELOG.md index 3779ad32..f78ef928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 5.0.3 18th April 2022 +- Fix: Reconnect to redis session on setup if it is closed. + ## 5.0.2 5th April 2022 - Fix: Create a dummy `AsyncstatsdClient` before connecting to real url, in case a connection cannot be made on init. - Fix: Move the creation of the `asyncio.Event`, `BotBase._guild_available` to within the setup hook, to avoid a deprecation notice. diff --git a/botcore/_bot.py b/botcore/_bot.py index e69b056d..69ccdc1c 100644 --- a/botcore/_bot.py +++ b/botcore/_bot.py @@ -222,6 +222,11 @@ class BotBase(commands.Bot): ) self.http.connector = self._connector + if getattr(self, "redis_session", False) and self.redis_session.closed: + # If the RedisSession was somehow closed, we try to reconnect it + # here. Normally, this shouldn't happen. + await self.redis_session.connect() + # Create dummy stats client first, in case `statsd_url` is unreachable within `_connect_statsd()` self.stats = AsyncStatsClient(loop, "127.0.0.1") self._connect_statsd(self.statsd_url, loop) @@ -265,7 +270,7 @@ class BotBase(commands.Bot): if self.stats._transport: self.stats._transport.close() - if getattr(self.redis_session, None): + if getattr(self, "redis_session", False): await self.redis_session.close() if self._statsd_timerhandle: diff --git a/pyproject.toml b/pyproject.toml index 76f0e667..1ad6edd9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "bot-core" -version = "5.0.2" +version = "5.0.3" description = "Bot-Core provides the core functionality and utilities for the bots of the Python Discord community." authors = ["Python Discord "] license = "MIT" -- cgit v1.2.3 From 8cff0d143fa64fd947ee9607745c32a6a96a7312 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Mon, 18 Apr 2022 22:38:49 +0100 Subject: Allow passing an api_client kwarg to BotBase --- CHANGELOG.md | 3 +++ botcore/_bot.py | 11 ++++++----- pyproject.toml | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'botcore/_bot.py') diff --git a/CHANGELOG.md b/CHANGELOG.md index f78ef928..47805c6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 5.0.4 18th April 2022 +- Feature: Allow passing an `api_client` to `BotBase`.`__init__` to specify the `botcore.site_api.APIClient` instance to use. + ## 5.0.3 18th April 2022 - Fix: Reconnect to redis session on setup if it is closed. diff --git a/botcore/_bot.py b/botcore/_bot.py index 69ccdc1c..90de58a5 100644 --- a/botcore/_bot.py +++ b/botcore/_bot.py @@ -40,6 +40,7 @@ class BotBase(commands.Bot): allowed_roles: list, http_session: aiohttp.ClientSession, redis_session: Optional[RedisSession] = None, + api_client: Optional[APIClient] = None, **kwargs, ): """ @@ -49,9 +50,10 @@ class BotBase(commands.Bot): guild_id: The ID of the guild use for :func:`wait_until_guild_available`. allowed_roles: A list of role IDs that the bot is allowed to mention. http_session (aiohttp.ClientSession): The session to use for the bot. - redis_session: The - ``[async_rediscache.RedisSession](https://github.com/SebastiaanZ/async-rediscache#creating-a-redissession)`` - to use for the bot. + redis_session: The `async_rediscache.RedisSession`_ to use for the bot. + api_client: The :obj:`botcore.site_api.APIClient` instance to use for the bot. + + .. _async_rediscache.RedisSession: https://github.com/SebastiaanZ/async-rediscache#creating-a-redissession """ super().__init__( *args, @@ -61,14 +63,13 @@ class BotBase(commands.Bot): self.guild_id = guild_id self.http_session = http_session + self.api_client = api_client if redis_session and RedisSession == discord.utils._MissingSentinel: warnings.warn("redis_session kwarg passed, but async-rediscache not installed!") elif redis_session: self.redis_session = redis_session - self.api_client: Optional[APIClient] = None - self._resolver: Optional[aiohttp.AsyncResolver] = None self._connector: Optional[aiohttp.TCPConnector] = None diff --git a/pyproject.toml b/pyproject.toml index 1ad6edd9..0815e65a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "bot-core" -version = "5.0.3" +version = "5.0.4" description = "Bot-Core provides the core functionality and utilities for the bots of the Python Discord community." authors = ["Python Discord "] license = "MIT" -- cgit v1.2.3