Skip to content

Commit

Permalink
Merge pull request #333 from mattcl/slack-reconnects
Browse files Browse the repository at this point in the history
Attempt to work around WebSocketConnectionClosedException for slack
  • Loading branch information
skoczen authored Mar 21, 2018
2 parents 64d8424 + 3a3da67 commit 1498a6b
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions will/backends/io_adapters/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import time
import traceback
import websocket

from markdownify import MarkdownConverter

Expand Down Expand Up @@ -438,31 +439,36 @@ def _update_backend_metadata(self):
self._update_channels()

def _watch_slack_rtm(self):
try:
if self.client.rtm_connect():
self._update_backend_metadata()

num_polls_between_updates = 30 / settings.EVENT_LOOP_INTERVAL # Every 30 seconds
current_poll_count = 0
while True:
events = self.client.rtm_read()
if len(events) > 0:
# TODO: only handle events that are new.
# print(len(events))
for e in events:
self.handle_incoming_event(e)

# Update channels/people/me/etc every 10s or so.
current_poll_count += 1
if current_poll_count > num_polls_between_updates:
self._update_backend_metadata()
current_poll_count = 0

self.sleep_for_event_loop()
except (KeyboardInterrupt, SystemExit):
pass
except:
logging.critical("Error in watching slack RTM: \n%s" % traceback.format_exc())
while True:
try:
if self.client.rtm_connect():
self._update_backend_metadata()

num_polls_between_updates = 30 / settings.EVENT_LOOP_INTERVAL # Every 30 seconds
current_poll_count = 0
while True:
events = self.client.rtm_read()
if len(events) > 0:
# TODO: only handle events that are new.
# print(len(events))
for e in events:
self.handle_incoming_event(e)

# Update channels/people/me/etc every 10s or so.
current_poll_count += 1
if current_poll_count > num_polls_between_updates:
self._update_backend_metadata()
current_poll_count = 0

self.sleep_for_event_loop()
except websocket.WebSocketConnectionClosedException:
logging.error('Encountered WebSocketConnectionClosedException attempting reconnect in 2 seconds')
time.sleep(2)
except (KeyboardInterrupt, SystemExit):
break
except:
logging.critical("Error in watching slack RTM: \n%s" % traceback.format_exc())
break

def bootstrap(self):
# Bootstrap must provide a way to to have:
Expand Down

0 comments on commit 1498a6b

Please sign in to comment.