-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbi.py
67 lines (44 loc) · 1.5 KB
/
dbi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/python3
import MySQLdb
import threading
import time
class dbi(threading.Thread):
def __init__(self, host, user, password, database):
super().__init__()
self.host = host
self.user = user
self.password = password
self.database = database
while True:
try:
self.reconnect()
self.probe()
break
except Exception as e:
print(f'Cannot connect to MySQL: {e}')
time.sleep(1)
self.name = 'GHBot MySQL'
self.start()
def reconnect(self):
try:
self.db = MySQLdb.connect(self.host, self.user, self.password, self.database, charset="utf8mb4", use_unicode=True)
cursor = self.db.cursor()
cursor.execute('SET NAMES utf8mb4')
cursor.execute("SET CHARACTER SET utf8mb4")
cursor.execute("SET character_set_connection=utf8mb4")
cursor.close()
except Exception as e:
print(f'dbi::reconnect: exception "{e}" at line number: {e.__traceback__.tb_lineno}')
def probe(self):
try:
cursor = self.db.cursor()
cursor.execute('SELECT NOW(), VERSION()')
cursor.fetchone()
cursor.close()
except Exception as e:
print(f'dbi::probe: MySQL indicated error: {e}')
self.reconnect()
def run(self):
while True:
self.probe()
time.sleep(29)