4 from retry import RetryLoop
14 from ws4py.client.threadedclient import WebSocketClient
16 _logger = logging.getLogger('arvados.events')
19 class _EventClient(WebSocketClient):
20 def __init__(self, url, filters, on_event, last_log_id, on_closed):
21 ssl_options = {'ca_certs': arvados.util.ca_certs_path()}
22 if config.flag_is_true('ARVADOS_API_HOST_INSECURE'):
23 ssl_options['cert_reqs'] = ssl.CERT_NONE
25 ssl_options['cert_reqs'] = ssl.CERT_REQUIRED
27 # Warning: If the host part of url resolves to both IPv6 and
28 # IPv4 addresses (common with "localhost"), only one of them
29 # will be attempted -- and it might not be the right one. See
30 # ws4py's WebSocketBaseClient.__init__.
31 super(_EventClient, self).__init__(url, ssl_options=ssl_options)
33 self.filters = filters
34 self.on_event = on_event
35 self.last_log_id = last_log_id
36 self._closing_lock = threading.RLock()
38 self._closed = threading.Event()
39 self.on_closed = on_closed
42 for f in self.filters:
43 self.subscribe(f, self.last_log_id)
45 def closed(self, code, reason=None):
49 def received_message(self, m):
50 with self._closing_lock:
52 self.on_event(json.loads(str(m)))
54 def close(self, code=1000, reason='', timeout=0):
55 """Close event client and optionally wait for it to finish.
57 :timeout: is the number of seconds to wait for ws4py to
58 indicate that the connection has closed.
60 super(_EventClient, self).close(code, reason)
61 with self._closing_lock:
62 # make sure we don't process any more messages.
64 # wait for ws4py to tell us the connection is closed.
65 self._closed.wait(timeout=timeout)
67 def subscribe(self, f, last_log_id=None):
68 m = {"method": "subscribe", "filters": f}
69 if last_log_id is not None:
70 m["last_log_id"] = last_log_id
71 self.send(json.dumps(m))
73 def unsubscribe(self, f):
74 self.send(json.dumps({"method": "unsubscribe", "filters": f}))
77 class EventClient(object):
78 def __init__(self, url, filters, on_event_cb, last_log_id):
81 self.filters = [filters]
84 self.on_event_cb = on_event_cb
85 self.last_log_id = last_log_id
86 self.is_closed = threading.Event()
87 self._setup_event_client()
89 def _setup_event_client(self):
90 self.ec = _EventClient(self.url, self.filters, self.on_event,
91 self.last_log_id, self.on_closed)
96 self.ec.close_connection()
99 def subscribe(self, f, last_log_id=None):
100 self.filters.append(f)
101 self.ec.subscribe(f, last_log_id)
103 def unsubscribe(self, f):
104 del self.filters[self.filters.index(f)]
105 self.ec.unsubscribe(f)
107 def close(self, code=1000, reason='', timeout=0):
109 self.ec.close(code, reason, timeout)
111 def on_event(self, m):
112 if m.get('id') != None:
113 self.last_log_id = m.get('id')
116 except Exception as e:
117 _logger.exception("Unexpected exception from event callback.")
118 thread.interrupt_main()
121 if not self.is_closed.is_set():
122 _logger.warn("Unexpected close. Reconnecting.")
123 for tries_left in RetryLoop(num_retries=25, backoff_start=.1, max_wait=15):
125 self._setup_event_client()
126 _logger.warn("Reconnect successful.")
128 except Exception as e:
129 _logger.warn("Error '%s' during websocket reconnect.", e)
131 _logger.exception("EventClient thread could not contact websocket server.")
133 thread.interrupt_main()
136 def run_forever(self):
137 # Have to poll here to let KeyboardInterrupt get raised.
138 while not self.is_closed.wait(1):
142 class PollClient(threading.Thread):
143 def __init__(self, api, filters, on_event, poll_time, last_log_id):
144 super(PollClient, self).__init__()
147 self.filters = [filters]
150 self.on_event = on_event
151 self.poll_time = poll_time
153 self.last_log_id = last_log_id
154 self._closing = threading.Event()
155 self._closing_lock = threading.RLock()
159 if self.last_log_id != None:
160 self.id = self.last_log_id
162 for f in self.filters:
163 for tries_left in RetryLoop(num_retries=25, backoff_start=.1, max_wait=self.poll_time):
165 items = self.api.logs().list(limit=1, order="id desc", filters=f).execute()['items']
167 except errors.ApiError as error:
173 _logger.exception("PollClient thread could not contact API server.")
174 with self._closing_lock:
176 thread.interrupt_main()
179 if items[0]['id'] > self.id:
180 self.id = items[0]['id']
182 self.on_event({'status': 200})
184 while not self._closing.is_set():
187 for f in self.filters:
188 for tries_left in RetryLoop(num_retries=25, backoff_start=.1, max_wait=self.poll_time):
190 items = self.api.logs().list(order="id asc", filters=f+[["id", ">", str(self.id)]]).execute()
192 except errors.ApiError as error:
198 _logger.exception("PollClient thread could not contact API server.")
199 with self._closing_lock:
201 thread.interrupt_main()
203 for i in items["items"]:
206 with self._closing_lock:
207 if self._closing.is_set():
211 except Exception as e:
212 _logger.exception("Unexpected exception from event callback.")
213 thread.interrupt_main()
214 if items["items_available"] > len(items["items"]):
218 self._closing.wait(self.poll_time)
220 def run_forever(self):
221 # Have to poll here, otherwise KeyboardInterrupt will never get processed.
222 while not self._closing.is_set():
223 self._closing.wait(1)
225 def close(self, code=None, reason=None, timeout=0):
226 """Close poll client and optionally wait for it to finish.
228 If an :on_event: handler is running in a different thread,
229 first wait (indefinitely) for it to return.
231 After closing, wait up to :timeout: seconds for the thread to
232 finish the poll request in progress (if any).
234 :code: and :reason: are ignored. They are present for
235 interface compatibility with EventClient.
238 with self._closing_lock:
241 self.join(timeout=timeout)
243 # "join() raises a RuntimeError if an attempt is made to join the
244 # current thread as that would cause a deadlock. It is also an
245 # error to join() a thread before it has been started and attempts
246 # to do so raises the same exception."
249 def subscribe(self, f):
250 self.on_event({'status': 200})
251 self.filters.append(f)
253 def unsubscribe(self, f):
254 del self.filters[self.filters.index(f)]
257 def _subscribe_websocket(api, filters, on_event, last_log_id=None):
258 endpoint = api._rootDesc.get('websocketUrl', None)
260 raise errors.FeatureNotEnabledError(
261 "Server does not advertise a websocket endpoint")
262 uri_with_token = "{}?api_token={}".format(endpoint, api.api_token)
264 client = EventClient(uri_with_token, filters, on_event, last_log_id)
266 _logger.warn("Failed to connect to websockets on %s" % endpoint)
272 def subscribe(api, filters, on_event, poll_fallback=15, last_log_id=None):
275 a client object retrieved from arvados.api(). The caller should not use this client object for anything else after calling subscribe().
277 Initial subscription filters.
279 The callback when a message is received.
281 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.
283 Log rows that are newer than the log id
286 if not poll_fallback:
287 return _subscribe_websocket(api, filters, on_event, last_log_id)
290 if not config.flag_is_true('ARVADOS_DISABLE_WEBSOCKETS'):
291 return _subscribe_websocket(api, filters, on_event, last_log_id)
293 _logger.info("Using polling because ARVADOS_DISABLE_WEBSOCKETS is true")
294 except Exception as e:
295 _logger.warn("Falling back to polling after websocket error: %s" % e)
296 p = PollClient(api, filters, on_event, poll_fallback, last_log_id)