X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/d446d49dd75f14f3b454fa89190068b4e475c946..83562734c781379665e3a337bce5b548afc88681:/sdk/python/arvados/events.py diff --git a/sdk/python/arvados/events.py b/sdk/python/arvados/events.py index f65486a7f1..09f2a871a9 100644 --- a/sdk/python/arvados/events.py +++ b/sdk/python/arvados/events.py @@ -1,24 +1,34 @@ -from ws4py.client.threadedclient import WebSocketClient -import threading +import arvados +import config +import errors + +import logging import json -import os +import threading import time -import ssl +import os import re -import config -import logging -import arvados +import ssl +from ws4py.client.threadedclient import WebSocketClient _logger = logging.getLogger('arvados.events') class EventClient(WebSocketClient): def __init__(self, url, filters, on_event): - ssl_options = None - if re.match(r'(?i)^(true|1|yes)$', - config.get('ARVADOS_API_HOST_INSECURE', 'no')): - ssl_options={'cert_reqs': ssl.CERT_NONE} + # Prefer system's CA certificates (if available) + ssl_options = {} + certs_path = '/etc/ssl/certs/ca-certificates.crt' + if os.path.exists(certs_path): + ssl_options['ca_certs'] = certs_path + if config.flag_is_true('ARVADOS_API_HOST_INSECURE'): + ssl_options['cert_reqs'] = ssl.CERT_NONE else: - ssl_options={'cert_reqs': ssl.CERT_REQUIRED} + ssl_options['cert_reqs'] = ssl.CERT_REQUIRED + + # Warning: If the host part of url resolves to both IPv6 and + # IPv4 addresses (common with "localhost"), only one of them + # will be attempted -- and it might not be the right one. See + # ws4py's WebSocketBaseClient.__init__. super(EventClient, self).__init__(url, ssl_options=ssl_options) self.filters = filters self.on_event = on_event @@ -55,6 +65,7 @@ class PollClient(threading.Thread): self.filters = [[]] self.on_event = on_event self.poll_time = poll_time + self.daemon = True self.stop = threading.Event() def run(self): @@ -102,29 +113,41 @@ class PollClient(threading.Thread): del self.filters[self.filters.index(filters)] +def _subscribe_websocket(api, filters, on_event): + endpoint = api._rootDesc.get('websocketUrl', None) + if not endpoint: + raise errors.FeatureNotEnabledError( + "Server does not advertise a websocket endpoint") + uri_with_token = "{}?api_token={}".format(endpoint, api.api_token) + client = EventClient(uri_with_token, filters, on_event) + ok = False + try: + client.connect() + ok = True + return client + finally: + if not ok: + client.close_connection() + def subscribe(api, filters, on_event, poll_fallback=15): - ''' - api: Must be a newly created from arvados.api(cache=False), not shared with the caller, as it may be used by a background thread. - filters: Initial subscription filters. - on_event: The callback when a message is received - poll_fallback: If websockets are not available, fall back to polling every N seconds. If poll_fallback=False, this will return None if websockets are not available. - ''' - ws = None - if 'websocketUrl' in api._rootDesc: - try: - url = "{}?api_token={}".format(api._rootDesc['websocketUrl'], api.api_token) - ws = EventClient(url, filters, on_event) - ws.connect() - return ws - except Exception as e: - _logger.warn("Got exception %s trying to connect to websockets at %s" % (e, api._rootDesc['websocketUrl'])) - if ws: - ws.close_connection() - if poll_fallback: - _logger.warn("Websockets not available, falling back to log table polling") - p = PollClient(api, filters, on_event, poll_fallback) - p.start() - return p - else: - _logger.error("Websockets not available") - return None + """ + :api: + a client object retrieved from arvados.api(). The caller should not use this client object for anything else after calling subscribe(). + :filters: + Initial subscription filters. + :on_event: + The callback when a message is received. + :poll_fallback: + If websockets are not available, fall back to polling every N seconds. If poll_fallback=False, this will return None if websockets are not available. + """ + + if not poll_fallback: + return _subscribe_websocket(api, filters, on_event) + + try: + return _subscribe_websocket(api, filters, on_event) + except Exception as e: + _logger.warn("Falling back to polling after websocket error: %s" % e) + p = PollClient(api, filters, on_event, poll_fallback) + p.start() + return p