Skip to content

Commit 86c2ff6

Browse files
author
Paul Decoursey
committed
https://github.com/WikiApiary/WikiApiary/issues/42
1 parent 256fbe7 commit 86c2ff6

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

apiary/ApiaryAPI/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def data(site_id):
1616

1717
duration = request.args.get('duration')
1818

19-
cur = apiary_db.cursor()
19+
cur = apiary_db.apiary_db.cursor()
2020
# SELECT capture_date, articles, pages FROM statistics WHERE website_id = :id AND capture_date > :date_filter
2121
temp_sql = "SELECT capture_date, articles, pages FROM statistics WHERE website_id = %d" % site_id
2222
cur.execute(temp_sql)

apiary/connect_mysql.py

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,62 @@
3030
else:
3131
LOGGER.info("No configuration file detected.")
3232

33-
LOGGER.info("Opening MySQL connection")
34-
apiary_db = mdb.connect(
35-
host=APIARYDB_HOSTNAME,
36-
db=APIARYDB_DATABASE,
37-
user=APIARYDB_USERNAME,
38-
passwd=APIARYDB_PASSWORD,
39-
charset='utf8')
33+
34+
35+
class ApiaryDB(object):
36+
37+
def __init__(self, host, database, username, password):
38+
self.host = host
39+
self.database = database
40+
self.username = username
41+
self.password = password
42+
self.reconnect()
43+
44+
def reconnect(self):
45+
LOGGER.info("Opening MySQL connection")
46+
self.apiary_db = mdb.connect(
47+
host=self.host,
48+
db=self.database,
49+
user=self.username,
50+
passwd=self.password,
51+
charset='utf8')
52+
53+
def fetch_one(self, sql, retry_count=0):
54+
try:
55+
cur = self.apiary_db.cursor()
56+
cur.execute(sql)
57+
return cur.fetchone()
58+
except MySQLdb.OperationalError:
59+
if retry_count > 10: # seemed like a good count
60+
raise Exception("max retries...")
61+
self.reconnect()
62+
retry_count += 1
63+
return self.fetch_one(sql, retry_count)
64+
except Exception, e:
65+
cur.close()
66+
LOGGER.error("SQL Command: %s" % sql)
67+
raise Exception(e)
68+
69+
70+
def runSql(self, sql_command, retry_count=0):
71+
"""Helper to run a SQL command and catch errors"""
72+
LOGGER.debug("SQL: %s" % sql_command)
73+
try:
74+
cur = self.apiary_db.cursor()
75+
cur.execute(sql_command)
76+
cur.close()
77+
self.apiary_db.commit()
78+
return True, cur.rowcount
79+
except MySQLdb.OperationalError:
80+
if retry_count > 10: # seemed like a good count
81+
raise Exception("max retries...")
82+
self.reconnect()
83+
retry_count += 1
84+
return self.runSql(sql_command, retry_count)
85+
except Exception, e:
86+
cur.close()
87+
LOGGER.error("SQL Command: %s" % sql_command)
88+
raise Exception(e)
89+
90+
apiary_db = ApiaryDB(APIARYDB_HOSTNAME, APIARYDB_DATABASE, APIARYDB_USERNAME, APIARYDB_PASSWORD)
4091

apiary/tasks/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ def runSql(self, sql_command):
7878
"""Helper to run a SQL command and catch errors"""
7979
LOGGER.debug("SQL: %s" % sql_command)
8080
try:
81-
cur = self.apiary_db.cursor()
82-
cur.execute(sql_command)
83-
cur.close()
84-
self.apiary_db.commit()
85-
return True, cur.rowcount
81+
return self.apiary_db.runSql(sql_command)
8682
except Exception, e:
8783
cur.close()
8884
LOGGER.error("SQL Command: %s" % sql_command)

apiary/tasks/bot/updatetotaledits.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def run(self):
3333
"""
3434

3535
# Get the total edit count
36-
cur = self.apiary_db.cursor()
37-
cur.execute(sql_query)
38-
data = cur.fetchone()
36+
data = self.apiary_db.fetch_one(sql_query)
3937
LOGGER.info("Total edits: %d Total active users: %d Total pages: %d", data[0], data[1], data[2])
4038

4139
# Update the wiki with the new value

0 commit comments

Comments
 (0)