Skip to content

Commit 4854be3

Browse files
committed
Slack RTM API reconnect loop fix
1 parent ee9c69e commit 4854be3

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

octoprint_Octoslack/__init__.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,32 +1252,31 @@ def execute_rtm_loop(self, slackAPIToken):
12521252

12531253
while sc == None or not sc.server.connected:
12541254
try:
1255+
##Roll over the counter to keep delay calculations under control
1256+
if connection_attempt > 100:
1257+
connection_attempt = 0
1258+
12551259
self._logger.debug(
12561260
"Attempting to connect Slack RTM API (iteration="
12571261
+ str(connection_attempt)
12581262
+ ")"
12591263
)
12601264

1261-
if sc == None:
1262-
wait_delay = 0
1263-
else:
1264-
wait_delay = self.get_rtm_reconnect_delay(
1265-
connection_attempt
1266-
)
1265+
wait_delay = self.get_rtm_reconnect_delay(connection_attempt)
12671266

12681267
if wait_delay > 0:
12691268
self._logger.debug(
12701269
"Sleeping for "
12711270
+ str(wait_delay)
12721271
+ " seconds before attempting connection"
12731272
)
1274-
time.sleep(wait)
1273+
time.sleep(wait_delay)
12751274

12761275
##Slack's client doesn't expose the underlying websocket/socket
12771276
##so we unfortunately need to rely on Python's GC to handle
12781277
##the socket disconnect
12791278
sc = SlackClient(slackAPIToken)
1280-
if sc.rtm_connect(with_team_state=False, auto_reconnect=True):
1279+
if sc.rtm_connect(with_team_state=False):
12811280
self._logger.debug(
12821281
"Successfully reconnected via Slack RTM API"
12831282
)
@@ -1288,8 +1287,7 @@ def execute_rtm_loop(self, slackAPIToken):
12881287
connection_attempt += 1
12891288
except Exception as e:
12901289
self._logger.error(
1291-
"Error Slack RTM API connection error (Exception): "
1292-
+ str(e)
1290+
"Slack RTM API connection error (Exception): " + str(e)
12931291
)
12941292
connection_attempt += 1
12951293

@@ -1326,13 +1324,22 @@ def execute_rtm_loop(self, slackAPIToken):
13261324
)
13271325

13281326
def get_rtm_reconnect_delay(self, iteration):
1329-
max_delay = 300 ##5 minutes
1327+
max_delay = 1800 ##30 minutes
13301328

1331-
delay = math.pow(2, iteration) * 5
1332-
if delay <= 0 or delay > max_delay:
1333-
return max_delay
1329+
try:
1330+
delay = (2 ** iteration) * 5
1331+
if delay <= 0 or delay > max_delay:
1332+
return max_delay
13341333

1335-
return delay
1334+
return delay
1335+
except Exception as e:
1336+
self._logger.exception(
1337+
"Slack RTM reconnect delay calculation error (iteration="
1338+
+ str(iteration)
1339+
+ "), Error: "
1340+
+ str(e.message)
1341+
)
1342+
return max_delay
13361343

13371344
def process_rtm_message(self, slackAPIToken, message):
13381345
if not self._settings.get(["slack_apitoken_config"], merged=True).get(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plugin_name = "Octoslack"
1515

1616
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
17-
plugin_version = "1.7.0"
17+
plugin_version = "1.7.1"
1818

1919
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
2020
# module

0 commit comments

Comments
 (0)