Skip to content

Commit 34cbd15

Browse files
authored
Stop waiting for update check on bot startup (#6687)
1 parent 9a458fd commit 34cbd15

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

redbot/core/_events.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,15 @@ async def _on_ready():
176176
if bot.intents.members: # Lets avoid 0 Unique Users
177177
table_counts.add_row("Unique Users", str(users))
178178

179-
outdated_red_message = ""
180-
rich_outdated_message = ""
181-
pypi_version, py_version_req = await fetch_latest_red_version_info()
182-
outdated = pypi_version and pypi_version > red_version_info
183-
if outdated:
184-
outdated_red_message, rich_outdated_message = get_outdated_red_messages(
185-
pypi_version, py_version_req
186-
)
179+
fetch_version_task = asyncio.create_task(fetch_latest_red_version_info())
180+
log.info("Fetching information about latest Red version...")
181+
try:
182+
await asyncio.wait_for(asyncio.shield(fetch_version_task), timeout=5)
183+
except asyncio.TimeoutError:
184+
log.info("Version information will continue to be fetched in the background...")
185+
except Exception:
186+
# these will be logged later
187+
pass
187188

188189
rich_console = rich.get_console()
189190
rich_console.print(INTRO, style="red", markup=False, highlight=False)
@@ -209,12 +210,23 @@ async def _on_ready():
209210
rich_console.print(
210211
f"Looking for a quick guide on setting up Red? Checkout {Text('https://start.discord.red', style='link https://start.discord.red}')}"
211212
)
212-
if rich_outdated_message:
213-
rich_console.print(rich_outdated_message)
214213

215214
bot._red_ready.set()
216-
if outdated_red_message:
217-
await send_to_owners_with_prefix_replaced(bot, outdated_red_message)
215+
216+
try:
217+
pypi_version, py_version_req = await fetch_version_task
218+
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
219+
log.error("Failed to fetch latest version information from PyPI.", exc_info=exc)
220+
except (KeyError, ValueError) as exc:
221+
log.error("Failed to parse version metadata received from PyPI.", exc_info=exc)
222+
else:
223+
outdated = pypi_version and pypi_version > red_version_info
224+
if outdated:
225+
outdated_red_message, rich_outdated_message = get_outdated_red_messages(
226+
pypi_version, py_version_req
227+
)
228+
rich_console.print(rich_outdated_message)
229+
await send_to_owners_with_prefix_replaced(bot, outdated_red_message)
218230

219231
@bot.event
220232
async def on_command_completion(ctx: commands.Context):

redbot/core/core_commands.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,11 @@ async def info(self, ctx: commands.Context):
424424
owner = app_info.owner
425425
custom_info = await self.bot._config.custom_info()
426426

427-
pypi_version, py_version_req = await fetch_latest_red_version_info()
427+
try:
428+
pypi_version, __ = await fetch_latest_red_version_info()
429+
except (aiohttp.ClientError, TimeoutError) as exc:
430+
log.error("Failed to fetch latest version information from PyPI.", exc_info=exc)
431+
pypi_version = None
428432
outdated = pypi_version and pypi_version > red_version_info
429433

430434
if embed_links:

redbot/core/utils/_internal_utils.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,29 @@ def expected_version(current: str, expected: str) -> bool:
326326
return Requirement(f"x{expected}").specifier.contains(current, prereleases=True)
327327

328328

329-
async def fetch_latest_red_version_info() -> Tuple[Optional[VersionInfo], Optional[str]]:
330-
try:
331-
async with aiohttp.ClientSession() as session:
332-
async with session.get("https://pypi.org/pypi/Red-DiscordBot/json") as r:
333-
data = await r.json()
334-
except (aiohttp.ClientError, asyncio.TimeoutError):
335-
return None, None
336-
else:
337-
release = VersionInfo.from_str(data["info"]["version"])
338-
required_python = data["info"]["requires_python"]
329+
async def fetch_latest_red_version_info() -> Tuple[VersionInfo, Optional[str]]:
330+
"""
331+
Fetch information about latest Red release on PyPI.
332+
333+
Raises
334+
------
335+
aiohttp.ClientError
336+
An error occurred during request to PyPI.
337+
TimeoutError
338+
The request to PyPI timed out.
339+
ValueError
340+
An invalid version string was returned in PyPI metadata.
341+
KeyError
342+
The PyPI metadata is missing some of the required information.
343+
"""
344+
async with aiohttp.ClientSession() as session:
345+
async with session.get("https://pypi.org/pypi/Red-DiscordBot/json") as r:
346+
data = await r.json()
347+
348+
release = VersionInfo.from_str(data["info"]["version"])
349+
required_python = data["info"]["requires_python"]
339350

340-
return release, required_python
351+
return release, required_python
341352

342353

343354
def deprecated_removed(

0 commit comments

Comments
 (0)