Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ Besides specifying `DEFAULT_REPLY` in `slackbot_settings.py`, you can also decor

```python
@default_reply
def my_default_hanlder(messsage):
def my_default_handler(messsage):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge conflict since it is already fixed.

message.reply('...')
```

Here is another variant of the decorator:

```python
@default_reply(r'hello.*)')
def my_default_hanlder(messsage):
def my_default_handler(messsage):
message.reply('...')
```

Expand Down
34 changes: 32 additions & 2 deletions slackbot/slackclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,39 @@ def parse_channel_data(self, channel_data):

def send_to_websocket(self, data):
"""Send (data) directly to the websocket."""
data = json.dumps(data)
self.websocket.send(data)

self.websocket_safe_send(data)
return

# BDOCTOR
#data = json.dumps(data)
#try:
# self.websocket.send(data)
#except Exception, e:
# logger.error('failed to send data to websocket connection, try to reconnect now')

def websocket_safe_send(self, data):
"""Safely send data to the websocket and retry on failure"""

data = json.dumps(data)
while True:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this while True, maybe consider making this a time based thing with exponential back off.

try:
self.websocket.send(data)
return
except WebSocketException as e:
logger.error('failed to send data to websocket connection, try to reconnect now')
if isinstance(e, WebSocketConnectionClosedException):
logger.warning('lost websocket connection, try to reconnect now')
else:
logger.warning('websocket exception: %s', e)
self.reconnect()
except Exception as e:
if isinstance(e, SSLError) and e.errno == 2:
pass
else:
logger.warning('Exception in websocket_safe_send: %s', e)
return

def ping(self):
return self.send_to_websocket({'type': 'ping'})

Expand Down