From 93fa795cad6c5ee1063549e93a245173a11617b5 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Tue, 28 Jul 2020 11:15:11 +0100 Subject: [PATCH] Made database connections Asynchronous --- settings.py | 55 +++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/settings.py b/settings.py index c789fdcd..432c58d3 100644 --- a/settings.py +++ b/settings.py @@ -1,5 +1,5 @@ +import asyncio import random -from contextlib import closing from discord import Colour @@ -56,6 +56,28 @@ def rndColour(): return Colour(random.randint(0, 0xFFFFFF)) +async def startup_cache_log(): + """Store the modlogs/prefixes in cache from the database on startup""" + + # Setup pool + pool = await db.connection(db.loop) + + # Setup up pool connection and cursor + async with pool.acquire() as conn: + async with conn.cursor() as cur: + # Grab the prefix of the server from the database + select_query = """SELECT * FROM guilds""" + + # Execute the query + await cur.execute(select_query) + results = await cur.fetchall() + + # Store the guildids and modlog channels + for row in results: + cache_modlogs(row[0], row[2]) + cache_prefix(row[0], row[1]) + + # Store guildID's and modlog channel within a cached dictionary modlogs = {} @@ -118,19 +140,6 @@ def get_modlog_for_guild(guildid): return channel -# Before initialising the cache. Store the prefixes from the database within the cache -with db.startup_connection() as conn: - # Grab the prefix of the server from the database - select_query = """SELECT * FROM guilds""" - with closing(conn.cursor()) as cursor: - # Execute the query - cursor.execute(select_query) - results = cursor.fetchall() - - # Store the guildids and modlog channels - for row in results: - cache_modlogs(row[0], row[2]) - # Storing the prefixes and guildID's in the cache cached_prefixes = {} @@ -176,19 +185,6 @@ def get_prefix_for_guild(guildid): return "defaultPrefix" -# Before initialising the cache. Store the prefixes from the database within the cache -with db.startup_connection() as conn: - # Grab the prefix of the server from the database - select_query = """SELECT * FROM guilds""" - with closing(conn.cursor()) as cursor: - # Execute the query - cursor.execute(select_query) - results = cursor.fetchall() - - # Store the guildids and prefixes within - for row in results: - cache_prefix(row[0], row[1]) - # Define repeated variables hammyMention = '<@154840866496839680>' hammyID = 154840866496839680 @@ -214,3 +210,8 @@ def extensions(): 'cogs.modmail', 'cogs.moderation'] return ext + + +# Run the async function to store everything in cache +loop = asyncio.get_event_loop() +loop.run_until_complete(startup_cache_log())