Skip to content

Commit

Permalink
Slack RTM API reconnect loop fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fraschetti committed Jan 26, 2019
1 parent ee9c69e commit 4854be3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
37 changes: 22 additions & 15 deletions octoprint_Octoslack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,32 +1252,31 @@ def execute_rtm_loop(self, slackAPIToken):

while sc == None or not sc.server.connected:
try:
##Roll over the counter to keep delay calculations under control
if connection_attempt > 100:
connection_attempt = 0

self._logger.debug(
"Attempting to connect Slack RTM API (iteration="
+ str(connection_attempt)
+ ")"
)

if sc == None:
wait_delay = 0
else:
wait_delay = self.get_rtm_reconnect_delay(
connection_attempt
)
wait_delay = self.get_rtm_reconnect_delay(connection_attempt)

if wait_delay > 0:
self._logger.debug(
"Sleeping for "
+ str(wait_delay)
+ " seconds before attempting connection"
)
time.sleep(wait)
time.sleep(wait_delay)

##Slack's client doesn't expose the underlying websocket/socket
##so we unfortunately need to rely on Python's GC to handle
##the socket disconnect
sc = SlackClient(slackAPIToken)
if sc.rtm_connect(with_team_state=False, auto_reconnect=True):
if sc.rtm_connect(with_team_state=False):
self._logger.debug(
"Successfully reconnected via Slack RTM API"
)
Expand All @@ -1288,8 +1287,7 @@ def execute_rtm_loop(self, slackAPIToken):
connection_attempt += 1
except Exception as e:
self._logger.error(
"Error Slack RTM API connection error (Exception): "
+ str(e)
"Slack RTM API connection error (Exception): " + str(e)
)
connection_attempt += 1

Expand Down Expand Up @@ -1326,13 +1324,22 @@ def execute_rtm_loop(self, slackAPIToken):
)

def get_rtm_reconnect_delay(self, iteration):
max_delay = 300 ##5 minutes
max_delay = 1800 ##30 minutes

delay = math.pow(2, iteration) * 5
if delay <= 0 or delay > max_delay:
return max_delay
try:
delay = (2 ** iteration) * 5
if delay <= 0 or delay > max_delay:
return max_delay

return delay
return delay
except Exception as e:
self._logger.exception(
"Slack RTM reconnect delay calculation error (iteration="
+ str(iteration)
+ "), Error: "
+ str(e.message)
)
return max_delay

def process_rtm_message(self, slackAPIToken, message):
if not self._settings.get(["slack_apitoken_config"], merged=True).get(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "Octoslack"

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

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

0 comments on commit 4854be3

Please sign in to comment.