@@ -14,7 +14,6 @@ class ThreadMySQL(ThreadBase):
1414 """ Polls mysql and inserts data into queue """
1515 is_running = True
1616 connection = None
17- reconnect_attempt = 0
1817 recovery_attempt = 0
1918 reconnect_delay = 5
2019 stats_checks = {}
@@ -55,11 +54,23 @@ def configure(self, config_dict):
5554 return self .host , self .port , self .sleep_interval
5655
5756 def setup_connection (self ):
58- try :
57+ connection_attempt = 0
58+
59+ while self .max_reconnect == 0 or connection_attempt <= self .max_reconnect :
60+ try :
5961 self .connection = mdb .connect (host = self .host , user = self .username , port = self .port , passwd = self .password )
6062 return self .connection
61- except Exception :
62- self .reconnect ()
63+ except Exception :
64+ pass
65+
66+ # If we got here, connection failed
67+ connection_attempt += 1
68+ time .sleep (self .reconnect_delay )
69+ print ('Attempting reconnect #{0}...' .format (connection_attempt ))
70+
71+ # If we get out of the while loop, we've passed max_reconnect
72+ raise ThreadMySQLMaxReconnectException
73+
6374
6475 def stop (self ):
6576 """ Stop running this thread and close connection """
@@ -131,15 +142,6 @@ def _preprocess(self, check_type, column_names, rows):
131142
132143 return executing_class .process (rows , * extra_args )
133144
134- def reconnect (self ):
135- if self .max_reconnect > 0 and self .reconnect_attempt >= self .max_reconnect :
136- raise ThreadMySQLMaxReconnectException
137-
138- self .reconnect_attempt += 1
139- print ('Attempting reconnect #{0}...' .format (self .reconnect_attempt ))
140- time .sleep (self .reconnect_delay )
141- self .setup_connection ()
142-
143145 def recover_errors (self , ex ):
144146 """Decide whether we should continue."""
145147 if self .max_recovery > 0 and self .recovery_attempt >= self .max_recovery :
@@ -161,10 +163,8 @@ def run(self):
161163 self .setup_connection ()
162164
163165 while self .is_running :
164- if self .connection .open :
165- self .reconnect_attempt = 0
166- else :
167- self .reconnect ()
166+ if not self .connection .open :
167+ self .setup_connection ()
168168
169169 try :
170170 self ._run ()
0 commit comments