mirror of
https://github.com/ansible/awx.git
synced 2026-04-13 22:19:27 -02:30
update awxkit to use new unsubscribe event
* Instead of waiting an arbitrary number of seconds. We can now wait the exact amount of time needed to KNOW that we are unsubscribed. This changeset takes advantage of the new subscribe reply semantic.
This commit is contained in:
committed by
Ryan Petrello
parent
feac93fd24
commit
3f2d757f4e
@@ -4,6 +4,7 @@ import logging
|
|||||||
import atexit
|
import atexit
|
||||||
import json
|
import json
|
||||||
import ssl
|
import ssl
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from six.moves.queue import Queue, Empty
|
from six.moves.queue import Queue, Empty
|
||||||
from six.moves.urllib.parse import urlparse
|
from six.moves.urllib.parse import urlparse
|
||||||
@@ -93,6 +94,7 @@ class WSClient(object):
|
|||||||
cookie=auth_cookie)
|
cookie=auth_cookie)
|
||||||
self._message_cache = []
|
self._message_cache = []
|
||||||
self._should_subscribe_to_pending_job = False
|
self._should_subscribe_to_pending_job = False
|
||||||
|
self._pending_unsubscribe = threading.Event()
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
wst = threading.Thread(target=self._ws_run_forever, args=(self.ws, {"cert_reqs": ssl.CERT_NONE}))
|
wst = threading.Thread(target=self._ws_run_forever, args=(self.ws, {"cert_reqs": ssl.CERT_NONE}))
|
||||||
@@ -184,11 +186,17 @@ class WSClient(object):
|
|||||||
payload['xrftoken'] = self.csrftoken
|
payload['xrftoken'] = self.csrftoken
|
||||||
self._send(json.dumps(payload))
|
self._send(json.dumps(payload))
|
||||||
|
|
||||||
def unsubscribe(self):
|
def unsubscribe(self, wait=True, timeout=10):
|
||||||
self._send(json.dumps(dict(groups={}, xrftoken=self.csrftoken)))
|
time_start = datetime.now()
|
||||||
# it takes time for the unsubscribe event to be recieved and consumed and for
|
if wait:
|
||||||
# messages to stop being put on the queue for daphne to send to us
|
# Other unnsubscribe events could have caused the edge to trigger.
|
||||||
time.sleep(5)
|
# This way the _next_ event will trigger our waiting.
|
||||||
|
self._pending_unsubscribe.clear()
|
||||||
|
self._send(json.dumps(dict(groups={}, xrftoken=self.csrftoken)))
|
||||||
|
if not self._pending_unsubscribe.wait(timeout):
|
||||||
|
raise RuntimeError(f"Failed while waiting on unsubscribe reply because timeout of {timeout} seconds was reached.")
|
||||||
|
else:
|
||||||
|
self._send(json.dumps(dict(groups={}, xrftoken=self.csrftoken)))
|
||||||
|
|
||||||
def _on_message(self, message):
|
def _on_message(self, message):
|
||||||
message = json.loads(message)
|
message = json.loads(message)
|
||||||
@@ -202,6 +210,10 @@ class WSClient(object):
|
|||||||
self._should_subscribe_to_pending_job['events'] == 'project_update_events'):
|
self._should_subscribe_to_pending_job['events'] == 'project_update_events'):
|
||||||
self._update_subscription(message['unified_job_id'])
|
self._update_subscription(message['unified_job_id'])
|
||||||
|
|
||||||
|
# unsubscribe acknowledgement
|
||||||
|
if 'groups_current' in message:
|
||||||
|
self._pending_unsubscribe.set()
|
||||||
|
|
||||||
return self._recv_queue.put(message)
|
return self._recv_queue.put(message)
|
||||||
|
|
||||||
def _update_subscription(self, job_id):
|
def _update_subscription(self, job_id):
|
||||||
|
|||||||
Reference in New Issue
Block a user