Skip to content

Commit 3de862c

Browse files
committed
Merge pull request #42 from craneworks/reconnect
Reconnect
2 parents b3ce811 + 9ff2906 commit 3de862c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

mysql_statsd/thread_mysql.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ThreadMySQL(ThreadBase):
1616
connection = None
1717
reconnect_attempt = 0
1818
recovery_attempt = 0
19+
reconnect_delay = 5
1920
max_reconnect = 30
2021
max_recovery = 10
2122
die_on_max_reconnect = True
@@ -55,8 +56,11 @@ def configure(self, config_dict):
5556
return self.host, self.port, self.sleep_interval
5657

5758
def setup_connection(self):
58-
self.connection = mdb.connect(host=self.host, user=self.username, port=self.port, passwd=self.password)
59-
return self.connection
59+
try:
60+
self.connection = mdb.connect(host=self.host, user=self.username, port=self.port, passwd=self.password)
61+
return self.connection
62+
except Exception:
63+
self.reconnect()
6064

6165
def stop(self):
6266
""" Stop running this thread and close connection """
@@ -134,6 +138,7 @@ def reconnect(self):
134138

135139
self.reconnect_attempt += 1
136140
print('Attempting reconnect #{0}...'.format(self.reconnect_attempt))
141+
time.sleep(self.reconnect_delay)
137142
self.setup_connection()
138143

139144
def recover_errors(self, ex):

0 commit comments

Comments
 (0)