mirror of
https://github.com/ansible/awx.git
synced 2026-05-09 02:17:37 -02:30
Wait for Slack RTM API websocket connection to be established
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# Copyright (c) 2016 Ansible, Inc.
|
# Copyright (c) 2016 Ansible, Inc.
|
||||||
# All Rights Reserved.
|
# All Rights Reserved.
|
||||||
|
|
||||||
|
import time
|
||||||
import logging
|
import logging
|
||||||
from slackclient import SlackClient
|
from slackclient import SlackClient
|
||||||
|
|
||||||
@@ -9,6 +10,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||||||
from awx.main.notifications.base import AWXBaseEmailBackend
|
from awx.main.notifications.base import AWXBaseEmailBackend
|
||||||
|
|
||||||
logger = logging.getLogger('awx.main.notifications.slack_backend')
|
logger = logging.getLogger('awx.main.notifications.slack_backend')
|
||||||
|
WEBSOCKET_TIMEOUT = 30
|
||||||
|
|
||||||
|
|
||||||
class SlackBackend(AWXBaseEmailBackend):
|
class SlackBackend(AWXBaseEmailBackend):
|
||||||
@@ -30,7 +32,18 @@ class SlackBackend(AWXBaseEmailBackend):
|
|||||||
if not self.connection.rtm_connect():
|
if not self.connection.rtm_connect():
|
||||||
if not self.fail_silently:
|
if not self.fail_silently:
|
||||||
raise Exception("Slack Notification Token is invalid")
|
raise Exception("Slack Notification Token is invalid")
|
||||||
return True
|
|
||||||
|
start = time.time()
|
||||||
|
time.clock()
|
||||||
|
elapsed = 0
|
||||||
|
while elapsed < WEBSOCKET_TIMEOUT:
|
||||||
|
events = self.connection.rtm_read()
|
||||||
|
if any(event['type'] == 'hello' for event in events):
|
||||||
|
return True
|
||||||
|
elapsed = time.time() - start
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
raise RuntimeError("Slack Notification unable to establish websocket connection after {} seconds".format(WEBSOCKET_TIMEOUT))
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self.connection is None:
|
if self.connection is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user