Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database to refactor #3

Merged
merged 1 commit into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fixed issue with static method referencing Utility object
  • Loading branch information
MujyKun committed Dec 26, 2020
commit 5359cee7a41295a7cbb96d92b17f32977bc82030
40 changes: 0 additions & 40 deletions Utility.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from module import keys, logger as log, cache, exceptions
from discord.ext import tasks
from PIL import Image
from concurrent.futures import ThreadPoolExecutor
from datadog import initialize, api
from Weverse.weverseasync import WeverseAsync
import datetime
Expand All @@ -13,7 +12,6 @@
import tweepy
import json
import time
import sys
import aiofiles
import re
import pytz
Expand Down Expand Up @@ -68,44 +66,6 @@ def __init__(self):
self.u_self_assign_roles = None
self.u_reminder = None




##################
# ## DATABASE ## #
##################
@tasks.loop(seconds=0, minutes=0, hours=0, reconnect=True)
async def set_start_up_connection(self):
"""Looping Until A Stable Connection to DB is formed. This is to confirm Irene starts before the DB connects.
Also creates thread pool and increases recursion limit.
"""
if self.client.loop.is_running():
try:
self.conn = await self.get_db_connection()
# Delete all active blackjack games
await self.delete_all_games()
self.running_loop = asyncio.get_running_loop()
await self.create_thread_pool()
sys.setrecursionlimit(self.recursion_limit)
except Exception as e:
log.console(e)
self.set_start_up_connection.stop()

async def create_thread_pool(self):
self.thread_pool = ThreadPoolExecutor()

@tasks.loop(seconds=0, minutes=1, reconnect=True)
async def show_irene_alive(self):
"""Looped every minute to send a connection to localhost:5123 to show bot is working well."""
source_link = "http://127.0.0.1:5123/restartBot"
async with self.session.get(source_link) as resp:
pass

@staticmethod
async def get_db_connection():
"""Retrieve Database Connection"""
return await keys.connect_to_db()

@staticmethod
def first_result(record):
"""Returns the first item of a record if there is one."""
Expand Down
6 changes: 4 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def __init__(self):

def run(self):
"""Start the bot."""
self.create_util_objects()
ex.initialize_data_dog() # initialize the class for DataDog metrics
# all active blackjack games are also deleted on db start, current session stats refreshed.
# cache is reset in the on_ready event.
ex.set_start_up_connection.start()
ex.u_database.set_start_up_connection.start()
if ex.test_bot:
self.run_test_bot()
else:
Expand Down Expand Up @@ -69,7 +70,7 @@ def start_loops(run_weverse=True):
# Send Packets to localhost:5123 to show Irene is alive. This is meant for auto restarting Irene
# This feature is essential in case of any overload or crashes by external sources.
# This also avoids having to manually restart Irene.
ex.show_irene_alive.start()
ex.u_database.show_irene_alive.start()

@staticmethod
def add_listeners():
Expand Down Expand Up @@ -115,6 +116,7 @@ def add_cogs():

@staticmethod
def create_util_objects():
"""Create SubClass Objects to attach to Parent for easier management and sharing between siblings."""
ex.u_database = util.database.DataBase()
ex.u_cache = util.cache.Cache()
ex.u_currency = util.currency.Currency()
Expand Down
38 changes: 37 additions & 1 deletion util/database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
from Utility import Utility
from module import logger as log
from discord.ext import tasks
from concurrent.futures import ThreadPoolExecutor
from module.keys import connect_to_db
import sys
import asyncio


class DataBase(Utility):
pass

@tasks.loop(seconds=0, minutes=0, hours=0, reconnect=True)
async def set_start_up_connection(self):
"""Looping Until A Stable Connection to DB is formed. This is to confirm Irene starts before the DB connects.
Also creates thread pool and increases recursion limit.
"""
if self.client.loop.is_running():
try:
self.conn = await self.get_db_connection()
# Delete all active blackjack games
await self.delete_all_games()
self.running_loop = asyncio.get_running_loop()
await self.create_thread_pool()
sys.setrecursionlimit(self.recursion_limit)
except Exception as e:
log.console(e)
self.set_start_up_connection.stop()

async def create_thread_pool(self):
self.thread_pool = ThreadPoolExecutor()

@tasks.loop(seconds=0, minutes=1, reconnect=True)
async def show_irene_alive(self):
"""Looped every minute to send a connection to localhost:5123 to show bot is working well."""
source_link = "http://127.0.0.1:5123/restartBot"
async with self.session.get(source_link) as resp:
pass

@staticmethod
async def get_db_connection():
"""Retrieve Database Connection"""
return await connect_to_db()