Skip to content

Commit

Permalink
enhance db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Apr 20, 2023
1 parent baaf3bf commit cddc37f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PyMySQL==1.0.3
celery==5.2.7
filetype==1.2.0
flower==1.2.0
psutil==5.9.4
psutil==5.9.5
influxdb==5.3.1
beautifulsoup4==4.12.2
fakeredis==2.10.3
Expand Down
11 changes: 8 additions & 3 deletions ytdlbot/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def commit(self):
def close(self):
pass

def ping(self, reconnect):
pass


class Cursor:
def __init__(self):
Expand All @@ -50,7 +53,8 @@ def __init__(self):
def execute(self, *args, **kwargs):
sql = self.sub(args[0])
new_args = (sql,) + args[1:]
return self.cur.execute(*new_args, **kwargs)
with contextlib.suppress(sqlite3.OperationalError):
return self.cur.execute(*new_args, **kwargs)

def fetchall(self):
return self.cur.fetchall()
Expand Down Expand Up @@ -245,13 +249,14 @@ class MySQL:
"""

def __init__(self):
if MYSQL_HOST:
try:
self.con = pymysql.connect(
host=MYSQL_HOST, user=MYSQL_USER, passwd=MYSQL_PASS, db="ytdl", charset="utf8mb4"
)
else:
except pymysql.err.OperationalError:
self.con = FakeMySQL()

self.con.ping(reconnect=True)
self.cur = self.con.cursor()
self.init_db()
super().__init__()
Expand Down

0 comments on commit cddc37f

Please sign in to comment.