12 from ws4py.client.threadedclient import WebSocketClient
14 _logger = logging.getLogger('arvados.events')
16 class EventClient(WebSocketClient):
17 def __init__(self, url, filters, on_event, last_log_id):
18 ssl_options = {'ca_certs': arvados.util.ca_certs_path()}
19 if config.flag_is_true('ARVADOS_API_HOST_INSECURE'):
20 ssl_options['cert_reqs'] = ssl.CERT_NONE
22 ssl_options['cert_reqs'] = ssl.CERT_REQUIRED
24 # Warning: If the host part of url resolves to both IPv6 and
25 # IPv4 addresses (common with "localhost"), only one of them
26 # will be attempted -- and it might not be the right one. See
27 # ws4py's WebSocketBaseClient.__init__.
28 super(EventClient, self).__init__(url, ssl_options=ssl_options)
29 self.filters = filters
30 self.on_event = on_event
31 self.last_log_id = last_log_id
32 self._closed_lock = threading.RLock()
36 self.subscribe(self.filters, self.last_log_id)
38 def received_message(self, m):
39 with self._closed_lock:
41 self.on_event(json.loads(str(m)))
43 def close(self, code=1000, reason=''):
44 """Close event client and wait for it to finish."""
45 super(EventClient, self).close(code, reason)
46 with self._closed_lock:
47 # make sure we don't process any more messages.
50 def subscribe(self, filters, last_log_id=None):
51 m = {"method": "subscribe", "filters": filters}
52 if last_log_id is not None:
53 m["last_log_id"] = last_log_id
54 self.send(json.dumps(m))
56 def unsubscribe(self, filters):
57 self.send(json.dumps({"method": "unsubscribe", "filters": filters}))
59 class PollClient(threading.Thread):
60 def __init__(self, api, filters, on_event, poll_time, last_log_id):
61 super(PollClient, self).__init__()
64 self.filters = [filters]
67 self.on_event = on_event
68 self.poll_time = poll_time
70 self.stop = threading.Event()
71 self.last_log_id = last_log_id
75 if self.last_log_id != None:
76 self.id = self.last_log_id
78 for f in self.filters:
79 items = self.api.logs().list(limit=1, order="id desc", filters=f).execute()['items']
81 if items[0]['id'] > self.id:
82 self.id = items[0]['id']
84 self.on_event({'status': 200})
86 while not self.stop.isSet():
89 for f in self.filters:
90 items = self.api.logs().list(order="id asc", filters=f+[["id", ">", str(self.id)]]).execute()
91 for i in items["items"]:
95 if items["items_available"] > len(items["items"]):
99 self.stop.wait(self.poll_time)
101 def run_forever(self):
102 # Have to poll here, otherwise KeyboardInterrupt will never get processed.
103 while not self.stop.is_set():
107 """Close poll client and wait for it to finish."""
113 # "join() raises a RuntimeError if an attempt is made to join the
114 # current thread as that would cause a deadlock. It is also an
115 # error to join() a thread before it has been started and attempts
116 # to do so raises the same exception."
119 def subscribe(self, filters):
120 self.on_event({'status': 200})
121 self.filters.append(filters)
123 def unsubscribe(self, filters):
124 del self.filters[self.filters.index(filters)]
127 def _subscribe_websocket(api, filters, on_event, last_log_id=None):
128 endpoint = api._rootDesc.get('websocketUrl', None)
130 raise errors.FeatureNotEnabledError(
131 "Server does not advertise a websocket endpoint")
133 uri_with_token = "{}?api_token={}".format(endpoint, api.api_token)
134 client = EventClient(uri_with_token, filters, on_event, last_log_id)
142 client.close_connection()
144 _logger.warn("Failed to connect to websockets on %s" % endpoint)
148 def subscribe(api, filters, on_event, poll_fallback=15, last_log_id=None):
151 a client object retrieved from arvados.api(). The caller should not use this client object for anything else after calling subscribe().
153 Initial subscription filters.
155 The callback when a message is received.
157 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.
159 Log rows that are newer than the log id
162 if not poll_fallback:
163 return _subscribe_websocket(api, filters, on_event, last_log_id)
166 return _subscribe_websocket(api, filters, on_event, last_log_id)
167 except Exception as e:
168 _logger.warn("Falling back to polling after websocket error: %s" % e)
169 p = PollClient(api, filters, on_event, poll_fallback, last_log_id)